【发布时间】:2018-11-27 00:56:15
【问题描述】:
这个问题与Eloquent save() method not working inside for loop有关。
过程图:上传文件 > 使用默认值 0 创建表行 > 继续迭代 0 部分(例如,如果最后一个 3-> 4,5,6)
我想在第一次上传图片后继续向特定项目添加图片。
$input['id']:持有项目id值。
我确实像这样应用 each() 方法:
*** code part executed after multiple file upload ***
TABLE STATUS AT FIRST STAGE :
+---------+-------------+-----------+
| id | project_id | order_id |
+---------+-------------+-----------+
| 1 | 15 | 1 |
+---------+-------------+-----------+
| 2 | 15 | 2 |
+---------+-------------+-----------+
| 3 | 16 | 1 | --> Added in the past
+---------+-------------+-----------+
| 4 | 16 | 0 | --> Created on first stage** : default 0.
+---------+-------------+-----------+
| 5 | 16 | 0 | --> Created on first stage** : default 0.
+---------+-------------+-----------+
*** code continues ***
$start = Images::select('order_id')->where('project_id', '=', $input['id'])
->orderBy('order_id', 'DESC')->first();
Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
->each(function($ord) use (&$start) {
$ord->order_id = $start++;
$ord->save();
});
上面给出的代码执行后的表格结果(仍在处理中)
+---------+-------------+-----------+
| id | project_id | order_id |
+---------+-------------+-----------+
| 1 | 15 | 1 |
+---------+-------------+-----------+
| 2 | 15 | 2 |
+---------+-------------+-----------+
| 3 | 16 | 1 | --> Added in the past
+---------+-------------+-----------+
| 4 | 16 | 0 | --> New / Present Action : 1st file
+---------+-------------+-----------+
| 5 | 16 | 0 | --> New / Present Action : 2nd one
+---------+-------------+-----------+
期望:
+---------+-------------+-----------+
| id | project_id | order_id |
+---------+-------------+-----------+
| 1 | 15 | 1 |
+---------+-------------+-----------+
| 2 | 15 | 2 |
+---------+-------------+-----------+
| 3 | 16 | 1 | --> Added in the past
+---------+-------------+-----------+
| 4 | 16 | 2 | --> New / Present Action : 1st file
+---------+-------------+-----------+
| 5 | 16 | 3 | --> New / Present Action : 2nd one
+---------+-------------+-----------+
有什么问题?错误的逻辑或 each() ?我不知道,但我尝试使用 foreach 并得到相同的错误结果。任何帮助将不胜感激。谢谢!
【问题讨论】: