我目前有1:1的关系,我需要它是一对一的。
这样Job Details
可以为1 Job Search
提供多个结果。
求职
public function up()
{
Schema::create('job_searches', function (Blueprint $table) {
$table->increments('id');
});
}
作业详情
public function up()
{
Schema::create('job_details', function (Blueprint $table) {
$table->integer('job_details_id',11);
$table->foreign('job_details_id')->references('id')->on('job_searches');
});
}
我得到的当前输出是:
Job_Search_Id
1
Job_Detail_Id
1
将另一个结果添加到job details
时,我得到:
Illuminate \ Database \ QueryException,消息为'SQLSTATE [23503]: 外键冲突:7 ERROR:在表上插入或更新 “job_details”违反了外键约束 “job_details_job_details_id_foreign”
我也说过我模特中的关系
职位搜索模型
class JobSearches extends Model
{
protected $primaryKey = 'id';
public function job_details(){
return $this->belongsToMany('App\job_details');
}
}
作业详情模型
class JobDetails extends Model
{
protected $primaryKey = 'job_details_id';
public function job_search(){
return $this->hasOne('App\job_search');
}
答案 0 :(得分:1)
将迁移更改为:
public function up()
{
Schema::create('job_details', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('job_search_id');
});
Schema::table('job_details', function (Blueprint $table) {
$table->foreign('job_search_id')->references('id')->on('job_searches');
});
}
JobSearch
类:
class JobSearch extends Model
{
public function jobDetails(){
return $this->hasMany('App\JobDetail');
}
}
JobDetail
类:
class JobDetails extends Model
{
public function jobSearch()
{
return $this->belongsTo('App\JobSearch');
}
}
如果你不经修改就使用代码,它将适合你。