Php 在《拉威尔·曼尼多姆》中的雄辩关系

Php 在《拉威尔·曼尼多姆》中的雄辩关系,php,laravel,orm,eloquent,relationship,Php,Laravel,Orm,Eloquent,Relationship,在我的Laravel项目中,我有三门课:城市、公司和活动。所以,城市有很多公司,而公司属于城市;然后我有一个公司可能有一个或多个活动;我想问的是,我怎样才能在一个城市里进行所有的活动 城市模式: public function companies() { return $this->hasMany('App\Models\Company'); } public function companies() { return $this-

在我的Laravel项目中,我有三门课:城市公司活动。所以,城市有很多公司,而公司属于城市;然后我有一个公司可能有一个或多个活动;我想问的是,我怎样才能在一个城市里进行所有的活动

城市模式:

public function companies()
    {
        return $this->hasMany('App\Models\Company');
    }
public function companies()
    {
        return $this->belongsToMany('App\Models\Company');
    }
公司模式:

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

public function activities()
    {
        return $this->belongsToMany('App\Models\Activity');
    }
活动模型:

public function companies()
    {
        return $this->hasMany('App\Models\Company');
    }
public function companies()
    {
        return $this->belongsToMany('App\Models\Company');
    }
谢谢你所做的一切。
致以最诚挚的问候。

您可以使用以下方法将城市模型加载到所有公司和活动中:

如果只想加载活动,可以使用以下方法: