laravel search multiple words separated by space

前端 未结 7 1981
鱼传尺愫
鱼传尺愫 2020-12-29 08:57

I am new to laravel query builder, I want to search multiple words entered in an input field for example if I type \"jhon doe\" I want to get any column that contains jhon o

7条回答
  •  渐次进展
    2020-12-29 09:33

    $string = 'john  doe';
    
    // split on 1+ whitespace & ignore empty (eg. trailing space)
    $searchValues = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY); 
    
    $users = User::where(function ($q) use ($searchValues) {
      foreach ($searchValues as $value) {
        $q->Where('name', 'like', "%{$value}%");
      }
    })->get();
    

    Using 'orWhere' will get you results for each keyword not results for keyword1 + keyword2...So all depends on what your looking for

提交回复
热议问题