在Laravel中使用分页动态地将where子句添加到查询中

Ela*_*ley 1 php laravel laravel-4

我有一个项目列表,我希望能够使用视图中的某些按钮进行过滤(显示,它们是活动的,只显示以某个字母开头的那些等),我对每个可能的组合进行了不同的查询一旦我有超过3个按钮进行过滤,那就失控了.

所以我想根据按下的按钮为我的初始查询添加一个where子句,但到目前为止我没有工作,我不知道它是否是分页:

(这是一个只有一个按钮的示例,我稍后会添加更多条件,但这已经不起作用了).

public function index()
{
    $hostesses = Hostess::orderBy('lastname', 'asc')
                ->paginate(30);

    if (Input::has('l')){
        $hostesses->where('lastname', 'like', Input::get('l').'%');
    }

    $this->layout->content = View::make('hostesses.index', compact('hostesses'));
}
Run Code Online (Sandbox Code Playgroud)

我在视图上收到此错误:

ErrorException

call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'where'
Run Code Online (Sandbox Code Playgroud)

Jes*_*ica 5

$hostesses = Hostess::orderBy('lastname', 'asc');

if (Input::has('l')){
    $hostesses->where('lastname', 'like', Input::get('l').'%');
}

$results = $hostessess->paginate(30);
Run Code Online (Sandbox Code Playgroud)

那样的东西?