繁体   English   中英

根据关系中最新模型的列,然后根据父模型的列过滤范围

[英]Filtering ranges based on a column from latest model in relationship, then a column from a parent model

我有两个表格: auctionsbids auctions ,我有一个start_amount字段,在bids ,有一个amount 这两个表还具有created_atmodified_at时间戳列。 我希望能够检索在特定范围内与最高(或最新价格,但您要查看的价格)匹配的拍卖列表。

我过去一周一直在努力,仅根据所有出价进行过滤,而不是根据最新(或最低)出价(包括start_amount )进行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();

那正是你想要的?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM