在这里,我想找到简化查询的解决方案,以便使用Laravel中的雄辩语言获取数据。
$room_id = Booking::whereBetween('from', [$request->from, $request->to])
->orWhereBetween('to', [$request->from, $request->to])
->where('from', '<=', $request->from, )
->where('to', '>=', $request->from)
->pluck('room_id');
$rooms = Room::whereNotIn('id', $room_id )->get();因此,在这里,我有两个雄辩的操作,以获得房间,但不包括在预定表中有特定的要求。到目前为止,我没有问题,但你们能给我最好的实践,以简化我的工作吗?谢谢。
发布于 2022-06-14 04:12:55
确保“预订”关系写在您的房间模型上。
$rooms = Room::whereDoesntHave('bookings', use($request) function($q){
$q->whereBetween('from', [$request->from, $request->to])
$q->orWhereBetween('to', [$request->from, $request->to])
$q->where('from', '<=', $request->from, )
$q->where('to', '>=', $request->from)
})->get();发布于 2022-06-14 04:21:42
您可以引用laravel关系将其添加到模型中,然后使用https://laravel.com/docs/9.x/eloquent-relationships查询连接表:
示例:
保护$with = 'product_savour‘;
公共函数product_savour() {返回$this->hasMany(ProductSavour::class,'product_id');}
$productQuery->whereHas(‘产品_品味’,函数($query)使用($filters) {$query-> use (‘品味’,函数($query)使用($filters) {$query-> use (类型),函数($query)使用($filters) {$query->其中(‘id’,$filters‘savour’;}););});
https://stackoverflow.com/questions/72611283
复制相似问题