如何在Laravel中计算变量并将变量附加到模型 [英] How to calculate and append a variable to a model in laravel

查看:403
本文介绍了如何在Laravel中计算变量并将变量附加到模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我的应用程序中有3个具有关系Hotel RoomPricing的模型,我需要显示每家酒店最便宜的房间.

Considering I have 3 models with relation Hotel Room and Pricing in my app, I need to show the cheapest room for each hotel.

我想计算价格并获得今天(今天是用户选择的一天的第一步)或当前任何日期的某个日期的最低价格,并获得所有价格中最低的价格将其追加到hotel模型中,以便以后可以在如下所示的spatie查询构建器程序包中使用它,以更清楚地了解要执行的操作:在下面粘贴我的Spatie查询构建器:

I want to calculate the pricing and get the lowest price for a certain date which is today (for first step it would be the user chosen day later on) or any date for now ,and get the lowest price room among all and append that to the hotel model so later on I can use it in my spatie query builder package like below to be more clear of what i want to do i paste my spatie query builder below :

 $data = QueryBuilder::for(Accommodation::class)
            ->allowedIncludes(['gallery','city','hotelRooms','hotelRooms.roomPricingHistorySearch'])
            ->allowedFilters([
            AllowedFilter::scope('bed_count'),
            AllowedFilter::scope('filter_price'),
            AllowedFilter::exact('city_id'),
            AllowedFilter::exact('is_recommended'),
            AllowedFilter::exact('accommodation_type_id'),
            'name',
            ])
            ->paginate(10);
        return $data;

推荐答案

假设您已经建立了获取酒店房间的关系(房间属于酒店,酒店有很多房间),并且一个房间的价格很多(取决于日期) ):

Assuming you have a relationship set up to get hotel rooms (room belongs to hotel, hotel has many rooms), and a room has many pricings (depending on date):

$hotels = Hotel::all();

$cheapestRooms = [];

foreach ($hotels as $hotel) {
    $roomIds = $hotel->rooms()->pluck('id')->toArray();

    $cheapestRoom[$hotel->name] = Room::whereIn('id', $roomIds)
        ->join('pricings', function ($join) {
            $join->on('room.id', '=', 'pricings.room_id')
                ->where('pricings.date', Carbon::now())
         })
        ->orderBy('room.price', 'asc')
        ->first();
}

这将创建一个关联数组,其中键是酒店名称,值是最便宜的房间.

This creates an associative array where the key is the hotel name and the value is the cheapest room.

要将其附加到您的酒店模型中,请在Hotel.php中进行

To append that to your Hotel model, in Hotel.php I would do:

public function cheapestRoom($date) {
    $roomIds = $this->rooms()->pluck('id')->toArray();

    return = Room::whereIn('id', $roomIds)
        ->join('pricings', function ($join) {
            $join->on('room.id', '=', 'pricings.room_id')
                ->where('pricings.date', $date)
         })
        ->orderBy('room.price', 'asc')
        ->first();
}

然后您可以像这样使用它:$hotel->cheapestRoom(Carbon::now);

And then you can use this like: $hotel->cheapestRoom(Carbon::now);

当需要日期范围时,可以将函数修改为2个日期并使用whereBetween.

When you want a date range, you can modify the function to take 2 dates and use whereBetween.

这篇关于如何在Laravel中计算变量并将变量附加到模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆