Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel update array syntax

Tags:

php

laravel

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');      
  }
like image 379
Yandra Adie Avatar asked Feb 26 '26 23:02

Yandra Adie


1 Answers

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(); 
}
like image 107
Rajinder Avatar answered Feb 28 '26 14:02

Rajinder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!