"从空值"
创建默认对象这是我的预订控制器
public function cancel($room_id, $booking_id) {
$booking = Booking::find($booking_id);
$room = Room::find($room_id);
$booking->status = 0;
$room->status = 1;
$booking->save();
$room->save();
session()->flash('msg','Booking has been canceled');
return redirect('/booking');
}
public function canceledBookings() {
$canceledBookings = Booking::where('status', 0)->get();
return view('bookings.canceled', compact('canceledBookings'));
}
当我取消对用户的预订时...页面返回错误 "从空值"
创建默认对象请帮助我......
答案 0 :(得分:0)
您在DB中没有具有给定ID的记录,其中一个返回null
:
$booking = Booking::find($booking_id);
$room = Room::find($room_id);
您可以查看null
:
if (is_null($booking))
或者您可以使用firstOrNew()
:
$booking = Booking::findOrNew($booking_id);
$room = Room::findOrNew($room_id);
如果没有记录,将创建一个新记录。
答案 1 :(得分:0)
不要使用查找更改,请参阅下面的代码 首先检查您的数据预订信息&房间信息
public function cancel($room_id, $booking_id) {
$booking = Booking::where('id',$booking_id)->first();
$booking->status = 0;
$booking->save();
$room = Room::where('id',$room_id)->first();
$room->status = 1;
$room->save();
session()->flash('msg','Booking has been canceled');
return redirect('/booking');
}