Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel How to update multiple records using request all

Tags:

laravel

How to update all records using request->all(), I have many columns to update. here is my code for create method which I am using to insert multiple new records which is working fine.

public function store(Request $request)
{
    $teacher = new Teacher;
    $teacher::create($request->all());
    $teacher->save();
    return back()->with('message','Teacher Added Successfully!');
}

here is what I tried but it's not working,

public function update(Request $request, Teacher $teacher)
{
    $teachers=$request->all();
    $teacher->save();
    return back()->with('message','Record Successfully Updated!');
}
like image 468
Fayakon Avatar asked Dec 07 '22 12:12

Fayakon


1 Answers

Instead of this:

public function update(Request $request, Teacher $teacher)
{
    $teachers = $request->all();
    $teacher->save();
    return back()->with('message', 'Record Successfully Updated!');
}

You have to try this:

public function update(Request $request, Teacher $teacher)
{
    $input = $request->all();
    $teacher->fill($input)->save();
    return back()->with('message', 'Record Successfully Updated!');
}

Assuming that you have to create fillable property in your Teacher model.

I hope it would be helpful. Thanks

like image 158
Inzamam Idrees Avatar answered Feb 12 '23 02:02

Inzamam Idrees