Php 对null Laravel函数调用成员函数update()
这个函数有什么问题?我在这行上有错误: $user->update$input;调用成员函数更新为nullPhp 对null Laravel函数调用成员函数update(),php,laravel,function,frameworks,Php,Laravel,Function,Frameworks,这个函数有什么问题?我在这行上有错误: $user->update$input;调用成员函数更新为null $input = $request->all(); if ($input == NULL) { return redirect()->route('users.index'); } else { $user = User::find($id); $user->update($input); DB::tabl
$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');
}
您可以检查用户是否有一些数据。或者,您可以查找失败而不是查找
很明显,它找不到具有该ID的用户。如果找不到,可以使用FindDorFail而不是find来执行404。错误告诉您,您正在对空对象调用->更新,因此$user为空,因此user::find$ID;返回null,因此没有id为$id的用户
$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');
}