我用User表和其他从Users表扩展的迁移创建了Laravel 5.4项目。但是,当我为用户创建个人数据时,不会出现错误,并且该数据也不会创建personal_details迁移。
控制器
<?php
public function store(Request $request)
{
//
$this->validate($request, [
'firstname' => 'required',
'middlename' => 'required',
'lastname' => 'required',
'gender' => 'required',
'nationality' => 'required',
'district_of_birth' => 'required',
'date_of_birth' => 'required',
'marital_status' => 'required',
'user_id' => 'required'
]);
PersonalDetail::create([
'firstname' => request('firstname'),
'lastname' => request('lastname'),
'middlename' => request('middlename'),
'gender' => request('gender'),
'nationality' => request('nationality'),
'date_of_birth' => request('date_of_birth'),
'district_of_birth' => request('district_of_birth'),
'marital_status' => request('marital_status'),
'user_id' => auth()->id()
]);
$personalDetail = new PersonalDetail;
$personalDetail->save();
return redirect()->route('pages.personaldata.index')
->with('success', 'Personal Details created successfully');
}
模型
class PersonalDetail extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
protected $fillable = [
'firstname',
'middlename',
'lastname',
'gender',
'nationality',
'district_of_birth',
'date_of_birth',
'marital_status',
'user_id'
];
}
迁移
public function up()
{
Schema::create('personal_details', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname', 255);
$table->string('lastname', 255);
$table->string('middlename', 255);
$table->string('gender', 255);
$table->string('nationality', 255);
$table->date('date_of_birth', 255);
$table->string('district_of_birth', 255);
$table->string('marital_status', 255);
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}