将var传递给雄辩的查询?

时间:2014-02-15 14:59:24

标签: php laravel laravel-4

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?它说这是未定义的?

2 个答案:

答案 0 :(得分: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'));
}

答案 1 :(得分: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'));