【发布时间】:2016-06-22 06:27:29
【问题描述】:
我有 4 张桌子。我想在一个表上实现查询并从相关表中获取数据。
在 CakePHP 中,我们使用 contain,但在 Laravel 中我不知道。
- 国家/地区,
- 状态,
- 城市,
- 位置
型号代码
class Country extends Model {
public function states() {
return $this->hasMany('App\State');
}
}
class State extends Model {
public function city() {
return $this->hasMany('App\City');
}
}
class City extends Model {
public function location() {
return $this->hasMany('App\Location');
}
}
class Location extends Model {
}
我必须对 Country 进行查询,并且我还想检索 State
$country = Country::where('id',1);
$country->states
这样,但我怎样才能得到cities -> location。我需要手动进行另一个查询吗?在 CakePHP 中我们使用 contain,但是在 Laravel 中,没有类似的关键字或功能?
【问题讨论】:
标签: php mysql laravel cakephp associations