Php 用户和国家/地区表之间的关系
我正在尝试获取用户国家/地区,但不起作用,我的示例代码是: 用户模型:Php 用户和国家/地区表之间的关系,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,我正在尝试获取用户国家/地区,但不起作用,我的示例代码是: 用户模型: public function countryName() { return $this->hasOne('App\Country'); } Tables: User: - id; - name; - country_id; Countries: -id ; - name; 将关系设置为: public function country() { return $this-
public function countryName()
{
return $this->hasOne('App\Country');
}
Tables:
User:
- id;
- name;
- country_id;
Countries:
-id ;
- name;
将关系设置为:
public function country()
{
return $this->belongsTo('App\Country');
}
要让国家做到这一点:
$user->country->name;
不工作,给我一个SQLSTATE错误[42S22]:未找到列:1054未知列“countries.user\u id”在“where子句”(SQL:select*from
countries
wherecountries
用户id=7和国家用户id
不是空限制1)(视图: