首页 > 解决方案 > laravel 在枢轴上具有多个模型关系

问题描述

我有一个州、县、销售团队和用户模型。

销售团队.php

public function state(){
        return $this->belongsTo(State::class);
    }
public function manager(){
        return $this->belongsTo(User::class,'manager_id');
    }

状态.php

public function counties(){
    return $this->hasMany('App\County')->orderBy('county');
}

县城.php

我想要实现的是,在某些情况下,可能会有一个子经理分配给一个销售团队,但只分配给一个特定的县。我创建了一个这样的表(我相信我需要它,如果我不这样做,请纠正我):

Schema::create('sub_managers', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('salesteam_id');
            $table->unsignedBigInteger('state_id');
            $table->unsignedBigInteger('submanager_id');                                                  
            $table->timestamps();
        });

我如何与这种类型的关系互动?我以为我需要这张桌子,因为它很有意义,但现在我不知道如何与它联系起来,我也必须制作模型吗?如果是这样,我如何将县与子经理联系起来以考虑分配给它的销售团队?

标签: laraveleloquentrelationship

解决方案


推荐阅读