Harry Singla
Harry Singla

Reputation: 33

How to use two where clauses with like operator in Laravel

I am using like operator two times in different where clauses. If I try one where clause, It works fine, but with both. It doesn't work.

Here is my code.

$products = Product::where(['is_live'=>'N'])->where(['is_active'=>'Y'])->where(['is_deleted'=>'N']);
$products->where('payment_mode', 'like', '%Credit/Debit Card%');
        $products->where('payment_mode', 'like', '%Cash%');
$products = $products->get();

Please help.

Upvotes: 1

Views: 49

Answers (1)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

You must be use orWhere

$products = Product::where(['is_live'=>'N'])
    ->where(['is_active'=>'Y'])
    ->where(['is_deleted'=>'N']) 
    ->where(function($query) {
        $query->where('payment_mode', 'like', '%Credit/Debit Card%')
              ->orwhere('payment_mode', 'like', '%Cash%');
    });

or you can use this

Product::where([
    'is_live'=>'N',
    'is_active'=>'Y',
    'is_deleted'=>'N'
])->where(function ($query) {
    $query
        ->where('payment_mode', 'like', '%Credit/Debit Card%')
        ->orwhere('payment_mode', 'like', '%Cash%');
})->get();

Upvotes: 1

Related Questions