2

I'm making a gallery with sortable photos with Laravel and jQuery UI Sortable.

My function in the controller gets a nice array:

$items = [0 => 22, 1 => 25, 2 => 45];

But there will be approx 150 - 200 photos in one gallery. Is there any chance to set one DB Query instead 150 - 200? Because my controller makes this at the moment...

<?php

foreach($photos['item'] as $position => $id){
    Photo::where('id', $id)->update(['position' => $position]);
}

But it creates approx 150 - 200 DB queries, which is awful.

Edit #1

Basically I need something like this (two corresponding arrays with ids and positions):

$ids =       [22, 24, 25, 34];
$positions = [0, 1, 2, 3];

Photos::where('id', $ids)->update(['position'] => $positions);

But I can't find anything about this approach.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95

1 Answers1

-2

Take a look here: Eloquent model mass update.

Basically, you are looking for a mass or bulk update.

Community
  • 1
  • 1
Jakub Kratina
  • 644
  • 6
  • 14