Laravel Controller: "for" loop getting value of first iteration only

For simplicity using the code provided by OP (at the time I am writing):

   $users = User::where('division', 1)
        ->where('has_been_assigned', null)
        ->get();
   foreach ($users as $user){
    $assignedLead = NewLeads::where('pic_endorsed_to', null)
        ->first();
    if(assignedLead){
        $assignedLead->pic_endorsed_to = $users->id;
        $assignedLead->save();
    }else{ break; }
   }

All I am doing here is just fetching the user and assigning those user's id to the NewLeads whose value is null.

In every loop, it will fetch the first row with pic_endorsed_to null and update the value to the user's id.


Its because you are not using the $x anywhere inside the loop to make it dynamic. Either use the $x in somewhere condition as per requirement or remove first.