2017-11-23 23 views
0

jQuery 데이터 테이블을 사용하여 목록을 만들고 싶습니다. 하지만 다음과 같은 사용법에 문제가 있습니다.Laravel belongsTo 메소드가있는 null 객체를 반환하는 관계

내 예약 모델 :

//Reserve Author 
    public function author(){ 

     return $this->belongsTo(User::class, 'author_id'); 

    } 

    //Reserver 
    public function reserver(){ 

     return $this->belongsTo(User::class, 'reserver_id'); 

    }  
    //Stock 
    public function stock(){ 

     return $this->belongsTo(Stock::class, 'reference_id', 'code')->where('company_id', $this->company_id); 

    } 

에 따라 사용이 성공적으로

$reserve = Reserve::find(id); 
$reserve->stock->name; 

을하고있다하지만 사용이 작동하지 않습니다 :

Reserve::with('reserver','author', 'stock')->get()->toJson(); 

예약하기 저자 관계가이 코드에서 작업 하지만 주식은 null 개체를 반환하고 있습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 네가 나를 도울 수 있으면 행복 할거야. 감사.

답변

0

나는 다른 방식으로 문제를 해결했습니다. 나는 각 방법에 익숙했고 현재 일하고 있습니다.

Reserve::with('reserver','author')->get()->each(function($q){ 
       $q->setAttribute('stock', $q->stock->toArray()); 
      })->toJson(); 

더 좋은 방법이 있다면 사용하고 싶습니다. 감사.