SQLSTATE [23000]: Integrity constraint violation: 19 NOT NULL constraint failed: in laravel
i having a problem can find a solution on their own.
I want to do for a model in i form, but i keep getting this error
SQLSTATE [23000]: Integrity constraint violation: 19 NOT NULL failure: drivers.user_id (SQL: insert into "drivers" ("vehicle_color", "plate_number", "join_date", "updated_at", "created_at") values (black, T701344C, 04/16/2017, 2017-04-16 18:32:04, 2017-04-16 18:32:04))
My controller
public function store(Request $request)
{
$this->validate($request, [
'first_name' => 'required|max:50',
'last_name' => 'required|max:50',
'phone_number' => 'required|numeric|unique:users',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'vehicle_id' => 'required',
'vehicle_color' => 'required',
'plate_number' => 'required',
]);
$user = new User();
$user->first_name = $request->input('first_name');
$user->last_name = $request->input('last_name');
$user->phone_number = $request->input('phone_number');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->sex = $request->input('sex');
$user->dob = $request->input('dob');
if($request->hasFile('avatar')){
$user->avatar = $request->file('avatar')->store('uploads/clients/avatar', 'public');
}
$user->dob = true;
$user->verified = true;
$user->save();
$user->driver()->create([
'vehicle_id' => $request->input('vehicle_id'),
'vehicle_color' => $request->input('vehicle_color'),
'plate_number' => $request->input('plate_number'),
'join_date' => Carbon::now()->format('m/d/Y'),
]);
$role = Role::whereName('driver')->first();
$user->assignRole($role);
alert()->success('Driver successfully added.')->autoclose(3000)->persistent('Close');
return redirect()->route('backend.drivers.index');
}
in the custom structure of the custom model
public function driver()
{
return $this->belongsTo(Driver::class);
}
in relation to the driver model
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
Driver table
Schema::create('drivers', function (Blueprint $table) {
$table->increments('id');
$table->integer('all_time_job')->nullable();
$table->string('vehicle_color')->nullable();
$table->string('plate_number')->nullable();
$table->date('join_date')->nullable();
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('vehicle_id')->unsigned()->index();
$table->foreign('vehicle_id')->references('id')->on('vehicles');
$table->timestamps();
});
Users table
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('phone_number')->unique();
$table->string('avatar')->nullable();
$table->string('dob')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->boolean('verified')->default(false);
$table->string('token')->nullable();
$table->rememberToken();
$table->timestamps();
});
Can someone help me with this
Stores user data in the user table, but does not store the driver date in the driver table.
I user laravel 5.4
source to share
Make sure "user_id" is in the $ fillable array of the Driver Model> class in another to prevent bulk assignment .
Like this
protected $fillable = [
...,
'user_id',
...,
];
source to share