【问题标题】:how to pick the lowest item in nested relationship in laravel如何在laravel中选择嵌套关系中的最低项目
【发布时间】:2019-09-04 01:18:03
【问题描述】:

我在这里有一个酒店模型、一个房间模型和一个房间价格模型我想通过我的嵌套关系查询以找到最便宜的房间,所以我必须获取今天房间的所有价格,然后对它们进行排序,从中选出最低的每个房间,然后为我的结果选择最低的房间:

在我的模型关系中,我选择每个房间所有价格中的最低价格:

// this will bring me the lowest price for the room in the day
public function roomPricingHistorySearch()
{
    return $this->hasOne(roomprices::class,'room_id','id')->orderBy('sales_price','ASC');
}

这里我想在所有房间中选择最低的

$data = Hotel::with([
    'rooms.roomprices' => function ($query) {
        //here i want to pick the lowest price room of all.
        $query->Where('is_deleted', '1');
    }
]);

【问题讨论】:

  • roomprices 查询的排序依据在哪里? (后一个查询)?另外,$query->where...,没有大写字母

标签: php laravel


【解决方案1】:

如果您按价格对房间进行排序,则获得该系列中的第一个。那将是最便宜的一个

代码示例

$product->ticketTypes()->hasActivePrices()->orderBy('price')‌​->first()

这将产生一个按价格排序的集合,最低的将是第一个。这就是为什么我们先做()

$data = Hotel::with([
    'rooms.roomprices' => function ($query) {

        $query->orderBy('sales_prices', 'ASC')->limit(1); // try this
    }
]);
``

【讨论】:

  • 不,我还没有对价格作为嵌套关系的房间进行排序,它不是要排序的房间对象,而是将第一个作为价格对象,所以我想比较所有房间的价格并选择最低
猜你喜欢
  • 1970-01-01
  • 2021-03-12
  • 2022-11-23
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2017-02-17
  • 2021-08-25
相关资源
最近更新 更多