2016-10-02 95 views
2

我有与vendordetails表hasOne关系的用户表。Laravel雄辩的关系获取记录到第三级关系

class User extends Authenticatable 
{ 
    public function vendor_details() { 
     return $this->hasOne('App\Vendordetail'); 
    } 
} 

另一方面vendordetails表包含COUNTRY_ID,STATE_ID和city_id,并与国家,州和城市模型属于关联关系。

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'); 
    } 

} 

我如何可以获取的国家,州和城市的记录,如果我的用户表中查询。

$user = User::with('vendor_details')->first(); 

在此查询中,它只查找vendordetails表的结果,我也希望国家,州和城市。

感谢

+0

尝试类似$ USER =用户::使用( 'vendor_details.othe_relationship') - >第一(); – Chintan7027

回答

4

,以便访问嵌套关系

$user = User::with(['vendor_details.this_state','vendor_details.this_country','vendor_details.this_city'])->first();

+0

Thankyouuuuu,我没有事件想这样 – Pankaj