【发布时间】:2016-03-07 20:29:44
【问题描述】:
我有 3 个模型:
城市:
protected $fillable = [
'name', 'latitude', 'longitude', 'code', 'country_id', 'status', 'weather_code',
];
public function translations() {
return $this->hasMany('App\Models\CityTranslation');
}
public function country() {
return $this->hasMany('App\Models\Country');
}
城市翻译
protected $fillable = [
'name', 'lang', 'city_id',
];
public function city() {
return $this->hasOne('App\Models\City', 'id', 'city_id');
}
国家
protected $fillable = [
'code', 'latitude', 'longitude', 'currency_id', 'timezone', 'dam_date', 'status',
];
public function city() {
return $this->hasMany('App\Models\City');
}
我的问题是,当我浏览我的 CityTranslations 并显示所选语言的城市名称时,我还想显示有关城市及其国家/地区的信息。
调用 $cityTranslation->city->longitude 没有问题,但是当我调用 $cityTranslation->city->country->code 时,它给了我一个 MySQL 错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'countries.city_id' in 'where clause' (SQL: select * from `countries` where `countries`.`city_id` = 4439 and `countries`.`city_id` is not null)
如何建立递归关系?
【问题讨论】:
-
为什么在城市和国家之间有一对多?一个城市可以有多个国家? oO 因此,它试图在国家/地区找到 city_id...
标签: php mysql laravel relationship laravel-5.2