Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel query builder or inside where

I have the following query and i want to know if this is possible in laravel's querybuilder:

SELECT * FROM table WHERE (column = value OR column = value2) AND column2 LIKE '%value3%'
like image 737
nusje2000 Avatar asked Dec 02 '16 12:12

nusje2000


2 Answers

Your query should look like this:

DB::table('table')
  ->where(function($q) use ($value, $value2) {
      $q->where('column', $value)
        ->orWhere('column', $value2);
  })
  ->where('column2', 'like', '%'.%value3.'%')
  ->get();

If you have multiple values, you can put them into a simple array and use whereIn():

DB::table('table')
  ->whereIn('column', $valuesArray)
  ->where('column2', 'like', '%'.%value3.'%')
  ->get();
like image 136
Alexey Mezenin Avatar answered Oct 21 '22 17:10

Alexey Mezenin


you can use closure in where for ex.

\DB::table('table_name')
 ->where(function($q){
       $q->where('column', 'value1')
         ->orWhere('column', 'value2');
  })
  ->where('column2', 'LIKE', '%value3%');

check here https://laravel.com/docs/5.3/queries#parameter-grouping

like image 38
Sagar Rabadiya Avatar answered Oct 21 '22 19:10

Sagar Rabadiya