2014-02-15 20 views
0
public function getShow($id, $take, $skip) 
{ 
    return $this->with(array('looks' => function($query){ 
     $query->wherePublished(1)->orderBy('sort')->take($take)->skip(0); 
    }))->whereId($id)->wherePublished(1)->get(array('id', 'title', 'short_title', 'thumb', 'banner', 'video')); 
} 

如何通過$ take?它說它是不確定的?將var傳遞給一個雄辯的查詢?

回答

2

這樣:

public function getShow($id, $take, $skip = 0) 
{ 
    return $this->with(array('looks' => function($query) use ($take, $skip) 
    { 
     $query->wherePublished(1)->orderBy('sort')->take($take)->skip($skip); 
    }))->whereId($id)->wherePublished(1)->get(array('id', 'title', 'short_title', 'thumb', 'banner', 'video')); 
} 
+0

語法錯誤,意外的「使用」(T_STRING),期待'{'' – panthro

+1

@panthro它是'使用'。小錯字,我糾正了它。 – SamV

+0

我可以在8分鐘內接受,謝謝。 – panthro

0

的另一種方法,可以使用scope是方法鏈接和靜態調用幫助:

你的模型:

public function scopeGetShow($qry, $id, $take, $skip = 0) 
{ 
    return $qry->with(array('looks' => function($query) use ($take, $skip) { 
     $query->wherePublished(1)->orderBy('sort')->take($take)->skip($skip); 
    }))->whereId($id)->wherePublished(1); 
} 

然後從使用控制器如:

$result = ModelName::getShow() 
     ->get(array('id', 'title', 'short_title', 'thumb', 'banner', 'video'));