我有3张桌子,
国家:身份证,姓名
省份:id、name、country_id
城市:id、name、province_id
我在模型中定义了如下关系,
国家型号:
public function Provinces()
{
return $this->hasMany('App\Province');
}
省份型号:
public function Country()
{
return $this->belongsTo('App\Country');
}
public function Cities()
{
return $this->hasMany('App\City');
}
城市模型:
public function Province()
{
return $this->belongsTo('App\Province');
}
我正在使用下面的查询,但它用国家名称覆盖了所有数据。
$city = DB::table('cities')
->join('provinces', 'cities.province_id', '=', 'provinces.id')
->join('countries', 'provinces.country_id', '=', 'countries.id')
->select('cities.name','provinces.name','countries.name')
->get();
我想从 laravel 5 中的这些表中仅获取城市名称、省份名称、国家名称的结果。你能帮我吗?