php - Call to a member function update() on null Laravel function
Get the solution ↓↓↓What is wrong with this function? I ve got error on this line:$user->update($input);
Call to a member function update() on null
$input = $request->all();
if ($input == NULL) {
return redirect()->route('users.index');
}
else {
$user = User::find($id);
$user->update($input);
DB::table('model_has_roles')->where('model_id', $id)->delete();
$user->assignRole($request->input('roles'));
return redirect()->route('users.index')
->with('success', 'User updated successfully');
}
Answer
Solution:
You can check if user has some data. Or you canfindOrFail()
instead offind()
.
$input = $request->all();
if ($input == NULL) {
return redirect()->route('users.index');
}
else {
$user = User::find($id);
if ($user) {
$user->update($input);
DB::table('model_has_roles')->where('model_id', $id)->delete();
$user->assignRole($request->input('roles'));
}
return redirect()->route('users.index')
->with('success', 'User updated successfully');
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: you must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.