2016-10-02 2 views
2

vendordetails 테이블과 hasOne 관계가있는 users 테이블이 있습니다. 다른 측면 vendordetails 테이블에Laravel eloquent relationship은 세 번째 수준의 관계를 가져옵니다.

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

는 country_id, STATE_ID 및 CITY_ID를 포함하고 국가, 주 및 도시 모델 belongsTo를의 관계를 가지고있다.

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

은 $ 사용자 = 사용자 같은 : ('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