TheUnreal
TheUnreal

Reputation: 24472

Laravel WHERE on same column

How I can query using Laravel Eloquent the following:

->where('items.type',"shirt")
->where('items.type',"glass")

I need to get the items with the type of shirt and glass

The above works with 1 Where on the same column. If I place the 2nd Where (glass), nothing is returned.

My desired query, as SQL:

WHERE items.type == "shirt" || items.type == "gass"

Upvotes: 0

Views: 1580

Answers (2)

abhishek.agarwal
abhishek.agarwal

Reputation: 36

If you want a OR condition, you should wrap it within a where!

->where(function($query){
   $query->where('items.type',"shirt")->whereOr('items.type',"glass");
});

OR

->whereIn('items.type', ['shirt', 'glass'])

Upvotes: 0

Marwelln
Marwelln

Reputation: 29413

Use the orWhere method.

->where('items.type', 'shirt')->orWhere('items.type', 'glass')

It's documented on https://laravel.com/docs/5.2/queries#where-clauses.

You may chain where constraints together, as well as add or clauses to the query. The orWhere method accepts the same arguments as the where method:

You can also use the whereIn method.

->whereIn('items.type', ['shirt', 'glass'])

The whereIn method verifies that a given column's value is contained within the given array:

Upvotes: 3

Related Questions