Yandra Adie Yandra Adie - 1 year ago 101
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">
<input type="checkbox" class="id" name="id[]" value="{{ $quarter->id }}" />
<input type="text" name="achv[]" value="{{ $quarter->achievement }}">

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]; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download