Eloquent save() method not working inside for loop

2019-08-04 08:23发布

I have a table like given below and this table is going to be used for project images.

id: primary key

project_id: a column that has correlation with projects.

order_num: represents image list order for specific project (eg. interior view of project, exterior view of construction project, a different aspect etc...)

+---------+-------------+-----------+
| id      | project_id  | order_num |  
+---------+-------------+-----------+
| 1       | 15          | 0         |
+---------+-------------+-----------+
| 2       | 15          | 0         |
+---------+-------------+-----------+
| 3       | 16          | 0         |
+---------+-------------+-----------+
| 4       | 16          | 0         |
+---------+-------------+-----------+
| 5       | 16          | 0         |
+---------+-------------+-----------+
                 .
                 .
                 .   

         GOES ON LIKE THIS


What i want to do:

+---------+-------------+-----------+
| id      | project_id  | order_num |  
+---------+-------------+-----------+
| 1       | 15          | 1         |
+---------+-------------+-----------+
| 2       | 15          | 2         |
+---------+-------------+-----------+
| 3       | 16          | 1         |
+---------+-------------+-----------+
| 4       | 16          | 2         |
+---------+-------------+-----------+
| 5       | 16          | 3         |
+---------+-------------+-----------+

I use Slim with Eloquent

I figured out something like this but i got bad method error for save() method.

$find = Images::where('project_id', '=', $project->id)->count();

$img = Images::where('project_id', '=', $project->id);

for ($i = 1; $i <= $find; $i++) {
     $img->order_num = $i;
     $img->save();
}

Am i doing something wrong? Any help would be very appreciated.

UPDATE [!!] Both answers are give the correct results but according to the odan's information, apokryfos's answer is clean code. Thanks for your all helps.

2条回答
\"骚年 ilove
2楼-- · 2019-08-04 08:35

try simply this

$imgs = Images::where('project_id', '=', $project->id)->get();
foreach($imgs as $i => $img) {
    $img->order_id = $i + 1;
    $img->save();
}
查看更多
\"骚年 ilove
3楼-- · 2019-08-04 08:48

$img seems to be a query builder object.

If you do $img = Images::where('project_id', '=', $project->id)->get(); you will get everything that needs updading example:

 $index = 1;
 Images::where('project_id', '=', $project->id)->get()
        ->each(function ($img) use (&$index) {
              $img->order_num = $index++;
              $img->save();
         });

This will update all image indices for a single project id. If you want to do all of them then you can do:

 $index = 0;
 $lastProjectId = null;
 Images::orderBy('project_id')->get()
        ->each(function ($img) use (&$index, $lastProjectId) {
              if ($lastProjectId == null) {
                 $lastProjectId = $img->project_id; 
              }   
              if ($img->project_id != $lastProjectId) {
                 $index = 1;
                 $lastProjectId = $img->project_id;
              }
              $img->order_num = $index++;
              $img->save();
         });
查看更多
登录 后发表回答