在Laravel中通过分页向查询动态添加where子句 [英] Adding where clause dynamically to a query with pagination in Laravel

查看:320
本文介绍了在Laravel中通过分页向查询动态添加where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目列表,我希望能够使用视图中的某些按钮进行过滤(显示,这些按钮处于活动状态,仅显示以某个字母开头的按钮,等等),而我正在针对每种可能的组合,只要我有3个以上的按钮要过滤,该组合就会失去控制.

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.

所以我想根据所按下的按钮在初始查询中添加一个where子句,但是到目前为止,我无法使用,也不知道是否是分页:

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'

推荐答案

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

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

$results = $hostessess->paginate(30);

类似的东西吗?

这篇关于在Laravel中通过分页向查询动态添加where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆