1 回答
TA贡献1802条经验 获得超10个赞
如果您使用 Unit 作为雄辩模型,请执行正常查询。您可以使用 with 来加载关系,如果您在 API 控制器中返回这些单位,则关系将被转换。基本上你的逻辑但是做到了雄辩的风格,可能有些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;
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报