如何使用 laravel 查询构建器在一个 select 中获取预订集合

How to get Collection of Reservations inside one select using laravel query builder

有以下代码,我想select根据单位

进行所有预订
$unitsQuery = DB::table('units as u')
        ->leftJoin('reservations as r', function ($join) use($fromDate,$toDate) {
            $join->on('u.id', '=', 'r.unit_id')
                ->where('r.team_id', auth()->user()->current_team_id)
                ->where('r.date_in ', '>=' , $fromDate)
                ->where('r.date_out ', '<=' , $toDate)
                ->whereNull('r.checked_out')
                ->whereNull('r.deleted_at')
                ->where('date_out', '!=', $fromDate)
                ->where('r.status' , '!=' , 'canceled')
                ->leftJoin('customer as c','r.customer_id' , '=' ,'c.id')
                ->leftJoin('wallets as w', function ($join) {
                    $join->on('r.id', '=', 'w.holder_id')
                        ->where('w.holder_type' , 'App\Reservation');
                });
        })
        ->select('u.id as uid',
            'u.name as uname',
            'u.team_id as utid',
            'u.unit_number as unum',
            'u.status as ustatus',
            'u.sunday_day_price as sunday_price',
            'u.monday_day_price as monday_price',
            'u.tuesday_day_price as tuesday_price',
            'u.wednesday_day_price as wednesday_price',
            'u.thursday_day_price as thursday_price',
            'u.friday_day_price as friday_price',
            'u.saturday_day_price as saturday_price',
            'u.month_price as month_price',

       
        )
        ->where('u.team_id' , auth()->user()->current_team_id)
        ->where('u.enabled' , 1)
        ->whereNull('u.deleted_at')
        ->orderBy('u.unit_number' , 'asc')
        ->groupBy(['u.id'])
        ->get();

我想要实现的是根据 $fromDate 和 $toDate 获得我所有的房间以及这个房间下的所有预订如何在一个 select 中实现这样的东西

   {
      "u.id":1,
      "reservations":[
         {
            "id":1,
            "price":200
         },
         {
            "id":2,
            "price":500
         }
      ]
   },
   {
      "u.id":2,
      "reservations":[
         {
            "id":3,
            "price":150
         },
         {
            "id":4,
            "price":150
         }
      ]
   }
]

如果您将 Unit 用作 eloquent 模型,请执行正常查询。您可以使用 with 来加载关系,如果您在 API 控制器中 return 这些单元,它们将因此被转换。基本上你的逻辑但是做的eloquent风格,可能会有一些SQL命名可能有错,但是基本思路是有的。

$units = Unit::query()
    ->leftJoin('reservations as r', function ($join) use($fromDate,$toDate) {
        $join->on('units.id', '=', 'r.unit_id')
            ->where('r.team_id', auth()->user()->current_team_id)
            ->where('r.date_in ', '>=' , $fromDate)
            ->where('r.date_out ', '<=' , $toDate)
            ->whereNull('r.checked_out')
            ->whereNull('r.deleted_at')
            ->where('date_out', '!=', $fromDate)
            ->where('r.status' , '!=' , 'canceled')
            ->leftJoin('customer as c','r.customer_id' , '=' ,'c.id')
            ->leftJoin('wallets as w', function ($join) {
                $join->on('r.id', '=', 'w.holder_id')
                    ->where('w.holder_type' , 'App\Reservation');
            });
    })
    ->select(
        'units.id as uid',
        'units.name as uname',
        'units.team_id as utid',
        'units.unit_number as unum',
        'units.status as ustatus',
        'units.sunday_day_price as sunday_price',
        'units.monday_day_price as monday_price',
        'units.tuesday_day_price as tuesday_price',
        'units.wednesday_day_price as wednesday_price',
        'units.thursday_day_price as thursday_price',
        'units.friday_day_price as friday_price',
        'units.saturday_day_price as saturday_price',
        'units.month_price as month_price',
    )
    ->where('units.team_id' , auth()->user()->current_team_id)
    ->where('units.enabled' , 1)
    ->whereNull('units.deleted_at')
    ->orderBy('units.unit_number' , 'asc')
    ->groupBy(['units.id'])
    ->with('reservations')
    ->get();

return $units;