@t0berius Sorry, I missed that.
You need the transactions of the orders that have currency 1 and either of one of the other options/filters, correct? Then the other options/filters must be in a parenthesis in the sql. This is why I used this
$qr->where(function($q) use ($search) {
$q
->where('address', $search)
->orWhere('paymentRef', $search)
->when(is_numeric($search), function ($qry) use ($search) {
$qry->orWhere('id', $search);
});
});
I put all the filters in one where, so Eloquent will put it in parenthesis.
In your case, I can't see other way except using joins.
$transactions = OrderTransaction::query()
->join('orders', 'order_transactions.order_id', '=', 'orders.id')
->select('order_transactions.*')
->where('orders.currency', 1)
->when($search != '', function($query) use ($search) {
$query->where(function($qr) use ($search) {
$qr
->where('orders.address', $search)
->orWhere('order_transactions.paymentRef', $search)
->when(is_numeric($search), function ($q) use ($search) {
$q->orWhere('order_transactions.id', $search);
});
});
});
If you use php8
$transactions = OrderTransaction::query()
->join('orders', 'order_transactions.order_id', '=', 'orders.id')
->select('order_transactions.*')
->where('orders.currency', 1)
->when(
$search != '',
fn($query) => $query->where(
fn($qr) => $qr
->where('orders.address', $search)
->orWhere('order_transactions.paymentRef', $search)
->when(is_numeric($search), fn($q) => $q->orWhere('order_transactions.id', $search))
)
);
This way you don't have to add the use ($search) all the time.
This is translated to the following sql
SELECT
order_transactions.*
FROM
order_transactions
INNER JOIN orders ON order_transactions.order_id = orders.id
WHERE
orders.currency = 1 AND
(
orders.address = 'search' OR
order_transactions.paymentRef = 'search' OR
order_transactions.id = search
)
It is important to put the address, paymentRef, id in parenthesis in order to search for the orders that have either one of these.
Since you only eager load the order id from the orders, I assume you do not need it or if you do you can get it from $transaction->order_id
Let me know if this works for you.