Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用户和国家/地区表之间的关系_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

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
where
countries
用户id=7和
国家
用户id
不是空限制1)(视图: