简体   繁体   English

同一模型上的 Laravel 多对多关系

[英]Laravel many-to-many relationship on the same model

I have a model Job .我有一个模型Job

A Job can require other Jobs be completed before it can begin.一个Job可以要求其他Jobs完成才能开始。

A Job may be the pre-required job for many Jobs at once.一个Job可能是许多前期工作需要Jobs一次。

So, say Job A depends on Job B and Job C , I would like to be able to call job->requiredJobs and get those two jobs.所以,说Job A取决于Job BJob C ,我希望能够调用job->requiredJobs并获得这两个工作。

As it stands, I have the following:就目前而言,我有以下几点:

class Job extends Model
{

    public function requiredJobs() 
    {
        return $this->hasMany('App\Job', 'required_job_id');
    }
}

However, I'm finding that if I create a Job D and have it require Job B and Job C , it overrides Job A 's required jobs field, since it seems to be adding required_job_id onto the required jobs, instead of creating an array on the dependent job.但是,我发现如果我创建一个Job D并让它需要Job BJob C ,它会覆盖Job A的 required jobs 字段,因为它似乎是将required_job_id添加到所需的作业中,而不是创建一个数组在依赖的工作上。

Hope this all makes sense!希望这一切都有意义! I'm not entirely sure if I need two definitions, or if hasMany is the wrong relationship ( belongsToMany instead?...)我不完全确定我是否需要两个定义,或者hasMany是否是错误的关系(而不是belongsToMany ?...)

Nick 's answer pushed me in the right direction.尼克的回答把我推向了正确的方向。

Here is how I ended up defining my model:这是我最终定义模型的方式:

class Job extends Model
{

    public function requiredJobs() 
    {
        return $this->belongsToMany('App\Job', null, 'dependent_job_ids', 'required_job_ids');
    }

    public function dependentJobs() 
    {
        return $this->belongsToMany('App\Job', null, 'required_job_ids', 'dependent_job_ids');
    }
}

This meant that when I call dependentJob->requiredJobs()->save(requiredJob) , a couple things happen:这意味着当我调用dependentJob->requiredJobs()->save(requiredJob) ,会发生一些事情:

  1. dependentJob gets an array of IDs required_job_ids , and I can call dependentJob->requiredJobs to get the entire list of job models. dependentJob获取一个 ID 数组required_job_ids ,我可以调用dependentJob->requiredJobs来获取整个工作模型列表。
  2. requiredJob gets an array of IDs dependent_job_ids , and I can call requiredJob->dependentJobs to get the entire list of job models. requiredJob获取一个 ID 数组dependent_job_ids ,我可以调用requiredJob->dependentJobs来获取整个作业模型列表。

Works like a charm!像魅力一样工作!

对于多对多关系belongsToMany()是正确的方法,但您还需要一个数据透视表来存储链接。

This is just to add on anyone looking for a solution on the same issue, relating the same model.这只是为了添加任何在同一问题上寻找解决方案的人,涉及同一模型。

Here you can create a joining table with normal Many to Many Relationships and attach ids as normal Laravel relationships在这里,您可以创建一个具有正常Many to Many关系的连接表,并将 id 附加为正常的 Laravel 关系

public function requiredJobs()
{
    return $this->belongsToMany('App\Job','dependent_jobs_required_jobs','dependent_job_ids','required_job_ids');
}

public function dependentJobs()
{
    return $this->belongsToMany('App\Job','dependent_jobs_required_jobs','required_job_ids','dependent_job_ids');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM