2014-02-23 3 views
0

어떤 이유로 모델 객체를 연결할 수 없습니다. 나는 'Order'에 대한 'Location'을 열렬하게로드하려고 노력하고 있으며, 모델 자체에 포함될 논리를 선호한다. 그러나 과거의 한 사슬은 작동하지 않습니다.Laravel 모델 객체 체인

class ChefController extends BaseController { 
    public function get_orders() { 
     $chef = $this->get_user_chef(); // this already works 
     return $chef->orders()->with('location')->get(); // does not work 
    } 
} 

답변

0

시도를 다음과 같이 두 번째 인수로 USER_ID를 추가하여 관계 (사용자 테이블)을 참조하기 :

class Order extends Eloquent { 
    protected $table = 'orders'; 

    public function customer() { 
     return $this->belongsTo('Customer'); 

    public function location() { 
     return $this->customer()->location(); // this does not work 
    } 
} 

class Customer extends Eloquent { 
    protected $table = 'customers'; 

    public function user() { 
     return $this->belongsTo('User'); 
    } 

    public function orders() { 
     return $this->hasMany('Order'); 
    } 

    public function location() { 
     return $this->user()->location(); 
      // return $this->user(); // WORKS!! 
    } 
} 

class User extends Eloquent { 
    protected $table = 'users'; 

    public function locations() { 
     return $this->hasMany('Location'); 
    } 

    public function location() { 
     return $this->locations()->first(); 
    } 
} 

나는 결국이 작업을 수행 할 수

public function user() { 
     return $this->belongsTo('User',"user_id"); 
} 

은 아마 당신은 전화 id 필드가 다르지만 내가 무슨 뜻인지 알 것입니다.

+0

이 기본값이 아닙니까? –

+0

글쎄, 당신이 다른 모델 이름을 당신의 이름, 당신은 id에 대해 명시해야합니다. – carousel

+0

메소드 이름은 모델과 다릅니다. –