laravel chaining orWhere() conditions

Viewed 17

Assuming the following eloquent query:

$transactions = OrderTransaction::with(['order:id']);

$transactions->whereHas('order', function($query) use ($search) {
    $query->currencyEUR()
    ->when($search != '', function($query) use ($search) {
        $query->where('address', $search);
    });
})
//only attach search by id if entered value is numeric
->when(is_numeric($search), function ($query) use ($search) {
    $query->orWhere('id', $search);
})
->when($search != '', function($query) use ($search) {
    $query->orWhere('paymentRef', $search);
});

The query will product a SQL statement:

select * from `order_transactions` where exists (select * from `orders` where `order_transactions`.`order_id` = `orders`.`id` and `currency` = 1 and `address` = ?) or `id` = ? or `paymentRef` = ?

How can I make sure condition currency = 1will be mandatory for every query? Otherwise it returns transactions with a currency !=currencyEUR()`. It seems like the chaining is incorrect.

0 Answers
Related