2014-02-24 38 views
3

我有两种型号,ThreadsLeadsLaravel - 将相关型号返回至json

我试图用线程作为JSON对象返回主角,但我得到的是一个leads为空的字段。

线程Model;

public function leads() 
{ 
    return $this->belongsTo('Leads'); 
}  

潜在模型;

public function threads() 
{ 
    return $this->hasMany('Threads'); 
} 

ThreadsController;

public function getLead($id=null) 
{ 
    $thread = Threads::thread($id)->with('leads')->get(); 

    return Response::json($thread)->setCallback(Input::get('callback')); 
} 

回答

2

相反的with(),尽量load()他们:

$thread = Threads::thread($id)->load('leads')->get(); 

此外,注意您namings:你Threads模型leads()功能应该被称为lead()因为Thread只拿到一个Lead(这就是为什么你使用belongsTo()),但这只是为了可读性。