我正在尝试学习本教程在function createReservation()
函数中:
while (strtotime($date) < strtotime($end_dt))
{
$room_calendar = RoomCalendar::where('day','=',$date)
->where('room_type_id','=',$room_info['id'])->first();
$night = ReservationNight::create();
$night->day=$date;
$night->rate=$room_calendar->rate;
$night->room_type_id=$room_info['id'];
$night->reservation_id=$reservation->id;
$room_calendar->availability--;
$room_calendar->reservations++;
$room_calendar->save();
$night->save();
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
$room_calendar->availability
没有默认值。那么当它减少时它不会变成负面的吗?
如果可用性的数据库列是无符号整数,它将变为负数,并且还会抛出错误。
您可以在查询中解决您的问题。当你查询$room_calendar
时,在你的while循环中
$room_calendar = RoomCalendar::where('day','=',$date)
->where('room_type_id','=',$room_info['id'])
->where('availability','>=',0)
->first();
之后还在代码中放置一个if块来检查$ room_calendar是否大于零。
像这样的东西
if($room_calendar->count())
{
$night = ReservationNight::create();
$night->day=$date;
$night->rate=$room_calendar->rate;
$night->room_type_id=$room_info['id'];
$night->reservation_id=$reservation->id;
$room_calendar->availability--;
$room_calendar->reservations++;
$room_calendar->save();
$night->save();
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
通过这种方式,您将无法获得任何零空间。
希望这会有所帮助。