Yandra Adie Yandra Adie - 2 months ago 21
PHP Question

Laravel update array syntax

I'm having trouble doing a record update array via POST in Laravel.

I have captured all the post data in an array cant update array achievement

<form action="{{'updateapp'}}" method="post">
<table>
<tr><td>
<input type="checkbox" class="id" name="id[]" value="{{ $quarter->id }}" />
<input type="text" name="achv[]" value="{{ $quarter->achievement }}">
</td></tr>
</table>
</form>


Controller :

public function foo(Request $request)
{
$ids = $request->id;
$achvs = $request->achv;
DB::table('quarters')->whereIn('id', $ids)
->update(array(['achievement' => $achvs ]));

return redirect('evaluator');
}

Answer Source

As you have set [] array in your form, you can access it as following

public function foo(Request $request)
{
    $ids = $request->id[0];
    $achvs = $request->achv[0];
    DB::table('quarters')->where('id', $ids)
    ->update(['achievement' => $achvs ]);   

    return redirect('evaluator');      
  }

if you want to update multiple rows then use following code:

foreach($request->id as $key => $value){ 

      $quarters = Quarters::find($request->id[$key]); 
      $quarters->achievement = $request->achv[$key]; 
      $quarters->save(); 
}