【发布时间】:2019-02-15 07:23:23
【问题描述】:
我有以下型号:
User:
- id
- name
Location:
- id
- name
- region_id
table: user_location
- user_id
_ location_id
用户通过该表属于ToMany 位置。我还有另一个模型:
Region
- id
- name
我定义了 Region hasMany Locations。
通过这些关系,我如何定义 User 和 Region 之间的关系,哪个 Region 将能够找到与其关联的所有 Locations 下的所有用户?
<?php
class User extends Model
{
public function locations() {
return $this->belongsToMany('App\Location', 'user_location');
}
}
class Location extends Model
{
public function users() {
return $this->belongsToMany('App\User', 'user_location');
}
public function region() {
return $this->belongsTo('App\Region', 'region_id');
}
}
class Region extends Model
{
public function locations() {
return $this->hasMany('App\Location', 'region_id');
}
public function users() {
// what am I supposed to put in here?
}
}
【问题讨论】: