在Laravel中为高级搜索组装查询

时间:2014-04-11 16:25:33

标签: php mysql laravel

好奇是否有办法解决为每个输入组合构建单独查询的问题,并具有高级搜索类型功能,如下所示:

if($market_sort && $company_sort && $search){
  $text_ads = Text_ad::where('company_id','=',$company_sort)->where('market_id','=',$market_sort)->where('text','like','%'.$search.'%')->paginate(15);
  }elseif($market_sort && $company_sort && !$search){
  $text_ads = Text_ad::where('company_id','=',$company_sort)->where('market_id','=',$market_sort)->paginate(15);
  } etc etc etc

我尝试了这种技术但是where子句执行查询并返回一个集合对象,因此无法再在其上调用paginate方法......

  $text_ads = new Text_ad;
  if($market_sort){
    $text_ads = $text_ads->where('market_id','=',$market_sort);
  }
  if($company_sort){
    $text_ads = $text_ads->where('company_id','=',$company_sort) 
  }
  $text_ads->paginate(15);

0 个答案:

没有答案