【发布时间】:2016-06-02 11:03:55
【问题描述】:
我制作了第三张表,因为我需要在第三张表中添加额外的列。 我在两个模型中都写了关系方法,但 id 不是 移动 在用户模型方法中是
class User extends Model
{
protected $fillable = [
'user_type_id', 'accountType', 'email', 'password', 'userName', 'gender', 'dob', 'country', 'city', 'mobileNo', 'cnic', 'address',
'degreeLevel', 'degreeTitle', 'institution', 'complitionYear', 'acedCountry', 'experience', 'workExperience', 'industry', 'perCountry', 'cv'
];
protected $table = 'users';
/********************************************************************/
/* Relationship between User and Ranklist */
/*******************************************************************/
public function RankList()
{
return $this->hasMany('App\Models\RankList','user_id');
}
public function Company()
{
return $this->hasMany('App\Models\Company');
}
public function newsAndEvents()
{
return $this->hasMany('App\Models\newsAndEvents','user_id');
}
/********************************************************************/
/* Relationship between User and user type */
/*******************************************************************/
public function UserType()
{
return $this->belongsTo('App\Models\UserType','user_type_id');
}
public function course_outline()
{
return $this->belongsTo('App\Models\CourseOuline','user_id');
}
public function Jobs()
{
return $this->belongsToMany('App\Models\Job','Job_User','User_id','Job_id');
}
public function Skill_User()
{
return $this->belongsToMany('App\Models\Skill','Skill_User','Skill_id','User_id');
}
public function Feedback_User()
{
return $this->belongsToMany('App\Models\Feedback','Feedback_User','Feedback_id','User_id');
}
}
in job model method is
class Job extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
/**************************************************/
/* Company Post Job */
/**************************************************/
protected $fillable = [
'jobType', 'jobTitle','skills','industry', 'department', 'vacancy', 'qualification', 'degreeTitle', 'miniExperience', 'jobCategory',
'city', 'gender', 'companyName', 'description','posting_date', 'applied_date', 'companyLogo',
];
protected $table = 'jobs';
/**************************************************/
/* Relationships between company and job */
/**************************************************/
public function Company()
{
return $this->belongsTo('App\Models\Company','company_id');
}
public function job_skill()
{
return $this->belongsToMany('App\Models\Skill','Job_Skill','Job_id','skill_id');
}
public function Users()
{
return $this->belongsToMany('App\Models\User','Job_User','User_id','Job_id');
}
}
pivot table code is
class Job_User extends Model
{
protected $fillable = [
'cv','current_salary','expected_salary','status',
];
protected $table = 'jobs_users';
}
但显示用户申请作业错误
完整性约束违规:1452 无法添加或更新子行:外键约束失败 (bridging_the_gap.jobs_users, CONSTRAINT jobs_users_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into jobs_users (cv, current_salary, expected_salary, updated_at, created_at) 值 (1st, 15,000-19,999, 30,000-39,999, 2016-02-6 2016-06-02 10:57:02 10:57:02))
【问题讨论】:
-
您的数据透视表的型号在哪里?你能提供那个代码吗?如果您没有数据透视表,请先检查this。
-
我没有在数据透视表中编写任何方法,但我有数据透视表。请告诉我方法
-
首先检查我第一条评论中的链接。然后如果有更多的问题问。
-
我已经根据给定的链接编写了方法,但错误是一样的
-
你有2个模型3个(包括枢轴模型)?如果有 3 个模型,则在问题中添加枢轴代码