雄辩的save()方法在for循环内不起作用 [英] Eloquent save() method not working inside for loop

查看:191
本文介绍了雄辩的save()方法在for循环内不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的表,该表将用于项目图像.

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

id:主键

project_id:与项目相关的列.

order_num:表示特定项目的图像列表顺序(例如,项目的内部视图,建筑项目的外部视图,其他方面等)

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

我发现类似这样的内容,但是save()方法出现错误的方法错误.

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.

更新[!!]这两个答案都给出了正确的结果,但是根据odan的信息,apokryfos的答案是干净的代码.感谢您的所有帮助.

推荐答案

$img似乎是查询生成器对象.

$img seems to be a query builder object.

如果您执行$img = Images::where('project_id', '=', $project->id)->get();,将会得到所有需要更新的示例:

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

这将更新单个项目ID的所有图像索引.如果您想全部完成,则可以执行以下操作:

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

这篇关于雄辩的save()方法在for循环内不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆