我在一个表中有预订列表,在另一个表中有事件信息,如:日期、日期、事件名称、价格、持续时间等。。
我需要使用days对预订进行分组,这很好,但当存在没有任何预订的预订点,并且我的join查询与pots和bookings表匹配时,问题就来了,但是如果某个给定的预订点没有任何预订,则该天的groupBy将失败。
我在周一、周二和周三都有斑点。如果星期一没有预订,我就得不到总数为零的预订。那天我什么都没有。总之,它不存在。
我需要的原因是:我在前台有一个进度条,上面显示每天的预订数量,所以如果当天没有预订,我就没有进度条了。
控制器:
//This query does not return bookings for tuesday because I dont have bookings.
$days = DB::table('spots')
->join('bookings','spots.id','=','bookings.spot_id')
->where('spots.event_id','=',$id)
->select('day',DB::raw('count(bookings.spot_id) as bookingcount'))
->groupby('spots.day')
->get();
视图:
@foreach($days as $day)
<div class="col-sm-6 col-md-3">
<div class="tablet tablet-stat">
<h4 class="tablet-header">{{ $day->day }}</h4>
<div id="circle">
<div class=" c100 p{{round(($day->bookingcount/$totalspace->totalspace)*100,0)}}">
<span> {{ round(($day->bookingcount/$totalspace->totalspace)*100,2) }} </span>
<div class="slice">
<div class="bar"></div>
<div class="fill"></div>
</div>
</div>
</div>
</div>
</div>
@endforeach
我并不是真的在寻找一个黑客,比如简单地找到没有预订的那一天,然后创建另一个foreach循环来显示那些没有预订的日子。我更喜欢在查询中将count设为零。
最佳答案
试试这个:
$days = DB::table('spots')
->leftJoin('bookings','spots.id','=','bookings.spot_id')
->where('spots.event_id','=',$id)
->select('day',DB::raw('count(bookings.spot_id) as bookingcount'))
->groupby('spots.day')
->get();
*修正了之前的答案,没什么意义
外部连接应该满足您的要求。
关于php - 在拉拉韦尔也获得零总预订的天数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37049344/