dongmei8071 2014-04-30 16:54
浏览 167
已采纳

在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'
  • 写回答

1条回答 默认 最新

  • doyz51819 2014-04-30 16:55
    关注
    $hostesses = Hostess::orderBy('lastname', 'asc');
    
    if (Input::has('l')){
        $hostesses->where('lastname', 'like', Input::get('l').'%');
    }
    
    $results = $hostessess->paginate(30);
    

    Something like that?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码