3

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');      
  }

2 Answers 2

8

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(); 
}
Sign up to request clarification or add additional context in comments.

3 Comments

FatalThrowableError in Grammar.php line 118: Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\xampp\htdocs\sofia-project\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 376
your question little confusing now. please explain in detail what do you want exactly.
I understood properly, I have update my answer. please check above code
0
public function foo(Request $request)
{
    $ids = $request->id[0];
    $achvs = $request->achv[0];
    DB::table('quarters')->where('id', $ids)
    ->update(array(['achievement' => $achvs,'achievement1' => $achvs1]));
    return redirect('evaluator');      
  }

1 Comment

While all answers are appreciated, it is very good to have some detail and explanation in your answer, so that people with similar problems can understand it too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.