根据关系中最新模型的列过滤范围,然后根据父模型的列过滤范围
Filtering ranges based on a column from latest model in relationship, then a column from a parent model
我有两个表:auctions
和 bids
。在 auctions
中,我有一个 start_amount
字段,在 bids
、amount
中。两个表还有 created_at
和 modified_at
时间戳列。我希望能够检索与特定范围内的最高(或最新,无论您如何看待)出价相匹配的拍卖列表。
过去一周我一直在努力,只成功地根据 所有 出价而不是最新(或最低)出价(start_amount
包括。)我将如何实现我正在寻找的东西?
我试过的代码:
if (request()->has('bid_range'))
$auctions->with(['bids' => function ($query) use ($bid_min, $bid_max) {
$query->whereBetween('amount', [$bid_min, $bid_max])->orWhereBetween('start_amount', [$bid_min, $bid_max])->orderBy('created_at', 'desc')->get();
}]);
如果我错了请纠正我,但你可能正在尝试这样做:
select *
from auctions
where exists
(select 1
from bids
where amount > 10 and amount < 20
and bids.auctions_id = auction.id)
因此,Laravel 方式:
DB::table('auctions')
->whereExists(function ($query) use ($bid_min, $bid_max) {
$query->from('bids')
->whereRaw("'amount' > $bid_min and 'amount' < $bid_max")
->whereRaw('bids.auctions_id = auction.id');
})
->get();
这就是你想要的?
我有两个表:auctions
和 bids
。在 auctions
中,我有一个 start_amount
字段,在 bids
、amount
中。两个表还有 created_at
和 modified_at
时间戳列。我希望能够检索与特定范围内的最高(或最新,无论您如何看待)出价相匹配的拍卖列表。
过去一周我一直在努力,只成功地根据 所有 出价而不是最新(或最低)出价(start_amount
包括。)我将如何实现我正在寻找的东西?
我试过的代码:
if (request()->has('bid_range'))
$auctions->with(['bids' => function ($query) use ($bid_min, $bid_max) {
$query->whereBetween('amount', [$bid_min, $bid_max])->orWhereBetween('start_amount', [$bid_min, $bid_max])->orderBy('created_at', 'desc')->get();
}]);
如果我错了请纠正我,但你可能正在尝试这样做:
select *
from auctions
where exists
(select 1
from bids
where amount > 10 and amount < 20
and bids.auctions_id = auction.id)
因此,Laravel 方式:
DB::table('auctions')
->whereExists(function ($query) use ($bid_min, $bid_max) {
$query->from('bids')
->whereRaw("'amount' > $bid_min and 'amount' < $bid_max")
->whereRaw('bids.auctions_id = auction.id');
})
->get();
这就是你想要的?