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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?