【发布时间】:2016-01-17 03:13:01
【问题描述】:
我写了这个,我只是想知道这是否是最好的方法。 订单有很多交易...交易属于订单。我正在查询 ID 大于发送到履行的最后一个 ID 的订单。那么我只想要已批准付款状态或离线付款类型的订单。
这段代码有效,只是想知道这是否是最好的方法:
/**
* 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` = ?)
谢谢!
【问题讨论】:
标签: php laravel eloquent relationship