I have two tables: auctions
and bids
. In auctions
, I have a start_amount
field and in bids
, amount
. Both tables also has created_at
and modified_at
timestamp columns. I want to be able to retrieve a list of auctions that match the highest (or latest, however you look at it) bid within a certain range.
I've been working at it for the past week and have only managed to filter based on all bids rather than the latest (or minimum) bid ( start_amount
included.) How would I achieve what I am looking for?
The code that I've tried:
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();
}]);
Correct me if I'm wrong, but you probably trying to do this:
select *
from auctions
where exists
(select 1
from bids
where amount > 10 and amount < 20
and bids.auctions_id = auction.id)
So, the Laravel way:
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();
That's what you want?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.