SQLSTATE [23000]:完整性约束违规:19 NOT NULL约束失败:在laravel
问题描述:
我有问题,可以找到解决方案我的自我。SQLSTATE [23000]:完整性约束违规:19 NOT NULL约束失败:在laravel
我想要做的模型在我的形式,但我不断收到此错误
SQLSTATE [23000]:完整性约束违规:19 NOT NULL约束失败:drivers.user_id(SQL:插入“驱动程序“(”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秒))
我的控制器
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');
}
在用户模型的用户关系
public function driver()
{
return $this->belongsTo(Driver::class);
}
在驱动程序模型关系
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
驱动程序表
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();
});
用户表
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();
});
有人可以帮我解决这个问题
它将用户数据保存在用户表中,但它不会将驱动程序日期保存在驱动程序表中。
我的用户laravel 5.4
答
确保“USER_ID”是$可填写阵列中其他你驱动程序模型类的,以防止mass assignable英寸
像这样
protected $fillable = [ ..., 'user_id', ..., ];
向我们展示您的架构此表 – RiggsFolly
我敢打赌没有'auto_increment'设置在列'user_id'(作为其主键) 。 – Dygnus
查看我的模式 –