Multiple column with same search string in laravel

前端 未结 3 1950
余生分开走
余生分开走 2021-01-21 23:24
SELECT * 
FROM table_name 
WHERE 
    CONCAT(id,name, address) LIKE \'%same_string%\' 

What is an alternate query for this in Laravel

相关标签:
3条回答
  • 2021-01-21 23:48

    Try this.

    $field = ['name','id','address'];
    $name = DB::Table('bookinfo')->Where(function ($query) use($string, $field) {
                 for ($i = 0; $i < count($field); $i++){
                    $query->orwhere($field, 'like',  '%' . $string .'%');
                 }      
            })->get();
    
    0 讨论(0)
  • 2021-01-21 23:58

    Try this for separate column

    DB::table("table_name")->whereRaw(" (`id` like ? or `name` like ? or `address` like ? ) ",["%".$same_string."%","%".$same_string."%","%".$same_string."%"])->get();
    
    0 讨论(0)
  • 2021-01-22 00:04

    ** Laravel - The general search in multiple columns the in single input

    **

    one of the user table in the first name, last name, job title column available and I have one input search in any value enter then find all column in associated data fetch and display

     $searchQuery = trim($request->query('search'));
     $requestData = ['firstname', 'lastname', 'job_title'];
      $user = User::where(function($q) use($requestData, $searchQuery) {
                            foreach ($requestData as $field)
                               $q->orWhere($field, 'like', "%{$searchQuery}%");
                    })->get();
    
    0 讨论(0)
提交回复
热议问题