【问题标题】:working on relation many to many in laravel 5.2在 laravel 5.2 中处理多对多关系
【发布时间】: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 个模型,则在问题中添加枢轴代码

标签: php sql database laravel


【解决方案1】:

试试这样的

//model jobs
public function Jobs()
{
    return $this->belongsToMany('App\Models\jobs_users');    
}

//model users
public function Users()
{
    return $this->belongsToMany('App\Models\jobs_users');  
}

//model pivot - jobs_users
public function user() {
    return $this->belongsTo('App\Models\user');
}

public function jobs() {
    return $this->belongsTo('App\Models\job');
}

这是制作枢轴(多对多关系)的基础。对于更多字段,您必须在数据透视模型中定义额外字段等...

希望对你有帮助

【讨论】:

  • 我已经编写了与要求尝试相同的代码,但到目前为止错误是一样的
  • 数据库中有那个数据透视表吗?在 User 和 Job 模型中具有相同的列 ID?
  • 在数据透视表中有两列 $table->integer('user_id')->unsigned(); $table->integer('job_id')->unsigned();
  • 将这些列添加到可填充模型 jobs_users 中,这样就可以了
  • 我应该在作业模型中添加作业方法或在作业模型中添加用户方法???请清除这种混乱
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 2016-11-03
  • 2023-04-06
  • 2016-12-20
  • 2016-05-05
  • 1970-01-01
相关资源
最近更新 更多