我有users表,它与vendordetails表有hasOne关系。
class User extends Authenticatable
{
public function vendor_details() {
return $this->hasOne('App\Vendordetail');
}
}
vendordetails表包含country_id、state_id和city_id,与Country、State和City模型有归属关系。
vendorDetail.php模型为:-
class Vendordetail extends Model
{
public $timestamps = false;
public function this_country(){
return $this->belongsTo('App\Country', 'country_id');
}
public function this_state(){
return $this->belongsTo('App\Country', 'state_id');
}
public function this_city(){
return $this->belongsTo('App\Country', 'city_id');
}
}
如果我查询users表,如何获取国家、州和城市的记录。
$user = User::with('vendor_details')->first();
在这个查询中,它只找到vendordetails表的结果,我还想要country、state和City。
谢谢
访问嵌套关系
$user=user::with(['vendor_details.this_state','vendor_details.this_country','vendor_details.this_city'])->first();