Search code examples
laraveleloquentlaravel-5.3

Save method not work in eloquent


I trying to update records so I use save() method, but record cannot update in database.

this is my code :

public function update(Request $request, $id)
    {
        $rules = [
            'email' => 'required|email',
            'firstname' => 'required',
            'lastname' => 'required'
        ];

        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        } else {
            $a = Users::find($id)->save($request->all());
        }

        return redirect()->to('admin/users/'.$id);
    }

What is the problem ?

P.S: create method work without problem.


Solution

  • Use update() method instead:

    Users::where('id', $id)->update($request->except(['_method', '_token']));
    

    Or try to use fill() to manually fill the data:

    $a = Users::find($id)->fill($request->all())->save();
    

    In both cases you're using mass assignment, so add all properties you're trying to update to $fillable array.