【发布时间】:2018-07-01 22:58:35
【问题描述】:
我有两个模型 Country 和 State。 它们之间的关系如下: 国家:
public function States()
{
return $this->hasMany('App\State');
}
状态:
public function Country()
{
return $this->belongsTo('App\Country');
}
现在,我想在 show 方法中获取属于该国家/地区的状态。
public function show(Country $country)
{
$states = $country->States()->get();
dd($states);
}
但是,这里会抛出一个错误:
SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列'states.country_id'(SQL:select * from states where states.country_id = 11 和 states.country_id 是不为空)
country_id 不存在是对的,因为它被命名为 countries_id 因为 Country 的表名称是国家。
请帮忙解决这个错误。
【问题讨论】:
-
states.country_id 引用表 states 和列 country_id,如果你想引用 Country 表使用 country。
标签: php laravel eloquent has-many