Laravel Eloquent Query Using Where With or and Or

Laravel Eloquent Query Using WHERE with OR AND OR?

Make use of Logical Grouping (Laravel 7.x/4.2). For your example, it'd be something like this:

Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});

How to Make Laravel Eloquent IN Query?

Here is how you do in Eloquent

$users = User::whereIn('id', array(1, 2, 3))->get();

And if you are using Query builder then :

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();

Laravel Eloquent Query Using WHERE a set of conditions or OR another set of conditions along with some common conditions?

Consider the following syntax:

DB::table('yourTable')
->where('x', '=', '1')
->where('y', '=', '1')
->where(function($q1)
{
$q1->where(function($q2)
{
$q2->where('a', '=', '1')
->where('b', '=', '1');
})
->orWhere(function($q2)
{
$q2->where('c', '=', '1')
->where('d', '=', '1');
});
});

If you prefer More Readable Syntax where will accept the array as Argument

DB::table('yourTable')
->where('x','=',1)
->where('y','=',1)
->where(function($queryBuilder){
$queryBuilder->where([
['a','=',1],
['b','=',1],
])
->orWhere([
['c','=',1],
['d','=',1],
]);
});

Laravel 5 Eloquent where and or in Clauses

Using advanced wheres:

CabRes::where('m__Id', 46)
->where('t_Id', 2)
->where(function($q) {
$q->where('Cab', 2)
->orWhere('Cab', 4);
})
->get();

Or, even better, using whereIn():

CabRes::where('m__Id', 46)
->where('t_Id', 2)
->whereIn('Cab', $cabIds)
->get();

Laravel Eloquent Query using Where, With, Where and orWhere condition

Try something like below:

$orders = Order::with('order_cart_products')
->where('user_id',auth()->user()->id)
->where(function ($query) use ($search) {
$query->where('order_id', 'LIKE', '%'.$search.'%')
->orWhereHas('order_cart_products', function ($query) use ($search) {
$query->where('p_name', 'LIKE', '%'.$search.'%')
})
})
->get();

Laravel eloquent multiple WHERE with OR AND OR and LIKE?

Use like and not like with % in where() and orWhere() methods:

->where('column', 'like', '%pattern%')

https://laravel.com/docs/5.3/queries#where-clauses

If you need to use multiple AND do like this:

->where($condition)
->where($anotherCondition)

If you want to use OR do this:

->where($condition)
->orWhere($anotherCondition)

To combine multiple AND and OR do parameter grouping:

->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})

Laravel Eloquent Query Using WHERE with AND orWhere

You need whereIn,

return PaymentSettings::whereIn('id', $paymentId)->get();

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

You can see detailed documentation here.

How to Create Multiple Where Clause Query Using Laravel Eloquent?

In Laravel 5.3 (and still true as of 7.x) you can use more granular wheres passed as an array:

$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])

Personally I haven't found use-case for this over just multiple where calls, but fact is you can use it.

Since June 2014 you can pass an array to where

As long as you want all the wheres use and operator, you can group them this way:

$matchThese = ['field' => 'value', 'another_field' => 'another_value', ...];

// if you need another group of wheres as an alternative:
$orThose = ['yet_another_field' => 'yet_another_value', ...];

Then:

$results = User::where($matchThese)->get();

// with another group
$results = User::where($matchThese)
->orWhere($orThose)
->get();

The above will result in such query:

SELECT * FROM users
WHERE (field = value AND another_field = another_value AND ...)
OR (yet_another_field = yet_another_value AND ...)

Laravel Eloquent query OR WHERE

What you are looking for is where() and orWhere() functions in Query Builder in laravel

$countries = Country::where('name', 'like', '%af%')
->orWhere('alpha-2', 'like', '%af%)
->orWhere('alpha-3', 'like', '%af%')
->get();

and if you want to dynamically pass values to where() functions.

$value = 'af';

$countries = Country::where('name', 'like', "%$value%")
->orWhere('alpha-2', 'like', "%$value%")
->orWhere('alpha-3', 'like', "%$value%")
->get();

Note 'Double Quotation' in variable passing.



Related Topics



Leave a reply



Submit