ronnysherer75's avatar

Laravel each method is not working

PROCESS MAP: UPLOAD FILES > CREATE TABLE ROWS WITH DEFAULT VALUE 0 > CONTINUE TO ITERATE OVER 0 PARTS(Eg If last one 3-> 4,5,6 )

I want to continue add images to the specific project after first image upload process.

$input['id']: holding project id value.

I do apply each() method like this:

*** 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 results after the code given above executed (still in process)

+---------+-------------+-----------+
| 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
+---------+-------------+-----------+

Expectation:

+---------+-------------+-----------+
| 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
+---------+-------------+-----------+

What's the problem? Wrong logic or each() ? i don't know but i tried with foreach and i got same wrong results. Any help would be very appreciated. Thanks !

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@ronnysherer75

Try accessing the id property when incrementing.

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

Please or to participate in this conversation.