nusje2000
nusje2000

Reputation: 503

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%'

Upvotes: 13

Views: 10848

Answers (3)

Bara' ayyash
Bara' ayyash

Reputation: 1925

You can do this according to make desired query

       DB::table('table_name')
      ->where('column', 'value1')
      ->orWhere('column', 'value2')
      ->where('column2', 'like', '%value3%');

Upvotes: 0

Sagar Rabadiya
Sagar Rabadiya

Reputation: 4321

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

Upvotes: 3

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

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();

Upvotes: 21

Related Questions