在Laravel中使用分页动态地将where子句添加到查询中
I have a list of items that I want to be able to filter using some buttons in the view (show, which are active, show only ones that start with a certain letter, etc) and I was making a different query for each possible combination and that was getting out of hand as soon as I have more than 3 buttons to filter.
So I want to add a where clause to my initial query depending on the buttons pressed, but what I have so far isn't working and I don't know if it's the pagination:
(This is an example with just one button, I'd add more conditions later but this isn't working already).
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'));
}
I get this error on the view:
ErrorException
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'where'
我有一个项目列表,我希望能够使用视图中的某些按钮进行过滤(显示, 是活动的,只显示以某个字母开头的那些,等等)我正在为每个可能的组合做出不同的查询,一旦我有超过3个按钮来过滤就会失控。 p> \ n
所以我想根据按下的按钮为我的初始查询添加一个where子句,但到目前为止我没有工作,我不知道它是否是分页: p> \ n
(这是一个只有一个按钮的示例,我稍后会添加更多条件,但这已经无效了。) p>
公共函数索引( )
{
$ hostesses = Hostess :: orderBy('lastname','asc')
- >> paginate(30);
if(Input :: has('l')){\ n $ hostesses-> where('lastname','like',Input :: get('l')。'%');
}
$ this-> layout-> content = View :: make('hostesses.index',compact('hostesses'));
}
code> p 重新>
我在视图上收到此错误: p>
ErrorException
call_user_func_array()期望参数1是有效的回调,类'Illuminate \ Support \ Collection'没有方法',其中'
code> pre>
div>
$hostesses = Hostess::orderBy('lastname', 'asc');
if (Input::has('l')){
$hostesses->where('lastname', 'like', Input::get('l').'%');
}
$results = $hostessess->paginate(30);
Something like that?