Я хочу обновить эти данные в базе данных.
ошибка:
ErrorException
Creating default object from empty value
Я получаю сообщение об ошибке в этой строке:
$user->name = $request->name;
Мой код контроллера:
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|max:255',
'mobile' => 'required|numeric|regex:/(0)[0-9]/|not_regex:/[a-z]/|digits:11',
'national_code' => 'required|numeric|regex:/(0)[0-9]/|not_regex:/[a-z]/|digits:10',
'avatar' => 'required|mimes:jpg,jpeg,png',
]);
$id = (int)$id;
$user = User::findOrFail($id);
$user->name = $request->name;
$user->mobile = $request->mobile;
$user->national_code = $request->national_code;
$user->province = $request->province;
$user->city = $request->city;
$user->address = $request->address;
$user->postcode = $request->postcode;
$user->active = $request->active;
$user->avatar = $request->avatar;
if(! is_null($request->password)) {
$request->validate([
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
$user->password = $request->password;
}
$user->active = $request->active ? 1 : 0;
if ($request->role_id) {
$user->syncRoles($request->role_id);
}
$user->save();
$notification = array(
'message' => 'Success !',
'alert-type' => 'success'
);
return redirect(route('management.users.index'))->with($notification);
}
Маршрут:
Route::resource('users', 'UserController');
Спасибо