2
我寫了這個,我只是想知道這是否是最好的方法。 訂單有很多交易...交易屬於訂單。我正在查詢訂單,ID比發送到履行的最後一個ID大。然後,我只需要已批准付款狀態或處於離線付款方式的訂單。Laravel複雜範圍查詢關係(供審查)
此代碼的工作,只是想知道如果它做到這一點的最好辦法:
/**
* Get all orders pending fulfilment
*
* @param $query
* @param $last_fulfiled
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeToFulfil($query, $last_fulfiled)
{
return $query->where('id', '>', $last_fulfiled)
->where(function($query) {
$query->whereHas('transactions', function ($query) {
$query->where('status', '>=', 5);
})->orWhere('payment_method', '=', 2);
});
}
編輯:
下面是SQL語法:
select * from `orders` where `orders`.`deleted_at` is null and `id` > ? and ((select count(*) from `transactions` where `transactions`.`order_id` = `orders`.`id` and `status` >= ?) >= 1 or `payment_method` = ?)
謝謝!
感謝您的回答,我將SQL語法添加到我的答案中......讓您意識到Laravel是多麼有用:) – JohnWolf
我認爲構建的查詢非常完美。只要你確定索引你過濾的列,那就太棒了。 –
是的,我做了...感謝您的善意幫助。 – JohnWolf