dsnw2651 2016-11-26 04:10
浏览 59
已采纳

或搜索中存在问题

Hi am searching my dashboard features

  • CATALOGUE STYLES
  • SAVED STYLES

The catalogue style is working perfectly but the saved style is searching based on the user, is there a way where is a andWhere() to search where(something) and where(another thing) in laravel.

public function search(){

    $wsSearch = Input::get('search');

    $term = $wsSearch;

    // search catalogue style

    $searchedCatalogueStylesCount = Cataloguestyle::where('catalogue_style_name','LIKE','%'.$term.'%')
    ->orWhere('catalogue_categories','LIKE','%'.$term.'%')
    ->orWhere('catalogue_style_description','LIKE','%'.$term.'%')
    ->orWhere('catalogue_sub_categories','LIKE','%'.$term.'%')
    ->count();

    $searchedCatalogueStyles = Cataloguestyle::where('catalogue_style_name','LIKE','%'.$term.'%')
    ->orWhere('catalogue_categories','LIKE','%'.$term.'%')
    ->orWhere('catalogue_style_description','LIKE','%'.$term.'%')
    ->orWhere('catalogue_sub_categories','LIKE','%'.$term.'%')
    ->paginate(50);

    // search saved style

    $searchedSaveStylesCount = Savestyle::where('user_id',Auth::user()->id)
    ->orWhere('catalogue_style_name','LIKE','%'.$term.'%')
    ->orWhere('catalogue_categories','LIKE','%'.$term.'%')
    ->orWhere('catalogue_style_description','LIKE','%'.$term.'%')
    ->orWhere('catalogue_sub_categories','LIKE','%'.$term.'%')
    ->count();

    $searchedSaveStyles = Savestyle::where('user_id',Auth::user()->id)
    ->orWhere('catalogue_style_name','LIKE','%'.$term.'%')
    ->orWhere('catalogue_categories','LIKE','%'.$term.'%')
    ->orWhere('catalogue_style_description','LIKE','%'.$term.'%')
    ->orWhere('catalogue_sub_categories','LIKE','%'.$term.'%')
    ->paginate(50);

    Session::flash('searchTerm',$term);

    // return Redirect::to('/search')
    return view('main_app.dashboard.search')
    ->with(['searchedCatalogueStyles' => $searchedCatalogueStyles,
            'searchedSaveStyles' => $searchedSaveStyles,
            'searchedCatalogueStylesCount' => $searchedCatalogueStylesCount,
            'searchedSaveStylesCount' => $searchedSaveStylesCount
            ]);

}

If you look closely at the $searchedSaveStyles variable, you will see where('user_id',Auth::user()->id), we want it to be a and the following where statements not or, how do we do that?

  • 写回答

1条回答 默认 最新

  • dousong9729 2016-11-26 04:47
    关注

    If you want it so that all the OR WHERE's are grouped then you would put them inside a WHERE with a function, like so:

    $searchedSaveStyles = Savestyle::where('user_id',Auth::user()->id)
      ->where(function($q) {
        $q->orWhere('catalogue_style_name','LIKE','%'.$term.'%')
          ->orWhere('catalogue_categories','LIKE','%'.$term.'%')
          ->orWhere('catalogue_style_description','LIKE','%'.$term.'%')
          ->orWhere('catalogue_sub_categories','LIKE','%'.$term.'%');
      })->paginate(50);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?