I
I
Ivan Zhuk2020-11-12 14:28:48
Laravel
Ivan Zhuk, 2020-11-12 14:28:48

How to update laravel data correctly?

Guys, can you tell me how to correctly update the data in Laravel,
I thought so, but the code does not work, you can correct me)

public function update(Request $request,$id){
        $user = Crud::find($id);

        $update = $user->update([
           'name' => $request->input('name'),
            'email' => $request->input('email')
        ]);
        $user->save();
        return redirect('/')->with('update', 'Вы успешно Изменили Данные');

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2020-11-12
@ivanzuk761

1) update itself calls save (save must be removed)
2) You can use magic and the final version will be like this

public function update(Request $request, Crud $crud){
        $crud->update([
            'name' => $request->get('name'),
            'email' => $request->get('email')
        ]);
        return redirect('/')->with('update', 'Вы успешно Изменили Данные');
    }

Important point:
- the $crud variable must be named as in the route
- use validation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question