Eloquent each() 方法 returns 零值

Eloquent each() method returns zero value

此问题与有关。

流程图:上传文件 > 创建 TABLE 具有默认值 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();
});

Table 上面给出的代码执行后的结果(仍在处理中)

+---------+-------------+-----------+
| 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,但得到了同样错误的结果。任何帮助将不胜感激。谢谢!

尝试在递增时访问 ID 属性。

Images::where('project_id', '=', $input['id'])->where('order_id', '=', 0)->get()
       ->each(function($ord) use (&$start) {
              $ord->order_id = $start->id++;
              $ord->save();
});
$start = Images::select('order_id')->where('project_id', '=', $input['id'])
->orderBy('order_id', 'DESC')->first();  

Returns Images

的对象

$start->order_id 应包含您的身份证号码