Eloquent save() method not working inside for loop

穿精又带淫゛_ 提交于 2019-12-24 08:22:05

问题


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.


回答1:


$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();
         });



回答2:


try simply this

$imgs = Images::where('project_id', '=', $project->id)->get();
foreach($imgs as $i => $img) {
    $img->order_id = $i + 1;
    $img->save();
}


来源:https://stackoverflow.com/questions/53470245/eloquent-save-method-not-working-inside-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!