Laravel 与多个自定义数据透视表的多对多关系?

标签 laravel laravel-5 eloquent many-to-many pivot-table

我希望我不是在问一个已经回答的问题,但是使用我能想到的所有搜索词,我无法找到解决方案:

我有两张 table ,companiesrespondents
有时,受访者可以 选择 一家或多家公司。一个公司可以是已选择 由一名或多名受访者提供。

其他时候,受访者可以 一家或多家公司。一个公司可以是评级 由一名或多名受访者提供。

一个受访者都可以选择 一家公司。 (确实,受访者必须选择所有对其进行评分的公司,但他们并未对他们选择的所有公司进行评分)。

更复杂的是,受访者是 员工 仅限一家公司,也是 提名 由一个或多个公司来选择和评级。

我的思路是有多个自定义枢轴:

//relationship between 'companies' and 'respondents' to show who they selected 
selected_companies 

//relationship between 'companies' and 'respondents' to show who they rated 
rated_companies 

//relationship between 'companies' and 'respondents' to show which companies a respondent was nominated by 
nominating_companies

//relationship between 'companies' and 'respondents' to show who a respondent is employed by
employment

我猜最后一个是简单的一对多,所以我可以放一个名为 employer_id 的自定义 FK在 respondents table 。

除此之外,我对如何实现这一点非常困惑。我知道自定义中间枢轴模型是一回事,但我也无法弄清楚如何实现它。

最佳答案

好的,这就是我如何处理这个问题的。
自定义数据透视表
在数据透视中,我添加了一个名为 type 的列。 .这是我的 company_respondent数据透视表迁移现在看起来像:

    Schema::create('company_respondent', function (Blueprint $table) {
        $table->unsignedInteger('company_id');
        $table->unsignedInteger('respondent_id');
        $table->string('type');

        $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
        $table->foreign('respondent_id')->references('id')->on('respondents')->onDelete('cascade');
        $table->primary(['company_id','respondent_id','type']);
    });
请注意,对于主键,我使用了所有三列。这将允许我声明同一公司-受访者对之间不同类型的关系,例如当受访者选择了一家公司我可以存储selected当他们对一家公司进行评级后,我可以存储 ratedtype柱子。
带枢轴
在我这样做之前,我需要告诉 Laravel 期待 Company 中的这个新列。和 Respondent使用 withPivot() 的模型在定义关系时。我需要在关系的双方都这样做:
//Respondent.php

    use App\Company;

    public function companies() 
    {
        return $this->belongsToMany(Company::class)->withPivot('type');
    }


//Company.php

    use App\Respondent;

    public function respondents() 
    {
        return $this->belongsToMany(Respondent::class)->withPivot('type');
    }
完成后,我现在可以在保存关系时存储到该列中,并使用它进行过滤。

储存:
$respondent->companies()->attach($companies_selected, ['type'=> 'selected']);
哪里$companies_selected是单个 ID 或 ID 数组。
过滤:
//returns an array of company names that a respondent with an id of `2` has selected.

$company_names = Respondent::find(2)
                    ->companies()
                    ->wherePivot('type','selected')
                    ->pluck('name')->toArray();
我可以简单地替换 selected , rated , nominated或者我喜欢定义两个表之间可能存在的不同类型的关系的任何其他东西。
我希望这对其他人有所帮助,或者我会获得有关执行此操作的更好方法的反馈。

关于Laravel 与多个自定义数据透视表的多对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704158/

相关文章:

laravel - 使用 vue.js 自动检查复选框值

php - 拉拉维尔 : Issue in database connection when use # keyword in env file

laravel-5 - 当 Laravel Passport 中的访问 token 过期时出现 MethodNotAllowedHttpException(密码授予类型)

php - 为什么我的 detach() 和 delete() 不起作用?

php - 在 Accordion 类中重复 div

php - Laravel:如何打开多个域(不是子域)以显示来自同一服务器的页面(无重定向)?

php - 供应商 :Publish - Nothing to publish for tag [] - Only on production server

php - Mysql Views - 自动增量不工作(通过 Eloquent ORM)

php - Eloquent Laravel - 仅当关系存在时才检索结果

php - Laravel Eloquent 非 lambda 参数