'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'email' cannot be null [LARAVEL]
I don't know what is wrong my code,please help I am almost finish it. I am using Laravel 7 btw
I received this message when I wanna update the phone number/email/password
Illuminate\Database\QueryException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'email' cannot be null (SQL:
update `users` set `email` = ?, `password` =
$2y$10$KZBGY4BFJIWzgh2wGhQ3yuZivuzo..MOLs2ahAYbXpecOUZsZ/nvC, `users`.`updated_at` = 2022-01-26
21:46:50 where `id` = 7)
Here is my update function
public function updateStaffDetails(Request $request, Staff $staff, User $user)
{
//$name = $request->input('stud_name');
$staff->update([
'staffPhone' => $request['staffPhone'],
'staffEmail' => $request['staffEmail'],
'staffPassword' => $request['staffPassword'],
]);
$user =Auth::user();
$user->update([
//'name' => $staff->staffName,
'email' => $staff->staffEmail,
'password'=> Hash::make($staff->staffPassword), //bcrypt($staff->staffPassword)
]);
$staff->user_id = $user->id;
$user->save();
$staff->save();
return redirect()->route('home',Auth::user())
->with('success','Staff details updated successfully.');
}
as you can see the Staff table will be copied to the users table once it is updated but the email from Users table is null...
here is my routes (because I think the problem is my route but i am not sure )
//admin show details
Route::get('/showDetailsAdm', 'StaffController@showDetailsAdm')->name('admins.showDetailsAdm');
//admin update
Route::put('/admins/updateStaffDetails', 'StaffController@updateStaffDetails')->name('admins.updateStaffDetails');
here is the picture of the column Staffs and Users user_id is FK from Users table
Solution 1:[1]
Your route must be like
Route::put('users/{user}/staffs/{staff}/update','StaffController@updateStaffDetails')->name('admins.updateStaffDetails');
Then you can get $user and $staff in your controller. Now, $staff isn't null so you can get email from it.
While calling to this route you need to pass user_id and staff_id as shown below:
route('admins.updateStaffDetails', [$user_id, $staff_id] )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Avinash Dengani |
