当[预订]中有数据时,我需要存储,但不存储任何内容以进行[扫描]
public function store(Request $request, $id){
$event = event::findOrFail($id);
$booking = booking::where('student_id',Auth::user()->student_id)
->where('name',Auth::user()->name)
->where('event_id',$event->id);
if($booking!=NULL){
$requestData = $request->all();
scan::create($requestData);
return redirect('event/' . $event->id .'/scan');
return view('event.scan', compact('event','scan','booking'));
}else{
return redirect('event/' . $event->id .'/scan');
}
}
但是使用$ booking == null时 它存储了无法预订的任何东西
答案 0 :(得分:3)
您的查询不完整。它缺少任何最终查询方法,例如get()
,first()
或exists()
在return
块中,您还有两个if
语句。只有第一个被执行,第二个将被忽略。
正确的代码应如下所示:
public function store(Request $request, $id){
$event = event::findOrFail($id);
$booking = booking::where('student_id',Auth::user()->student_id)
->where('name',Auth::user()->name)
->where('event_id',$event->id)
->get(); // look this line
if($booking!=NULL){
$requestData = $request->all();
scan::create($requestData);
return redirect('event/' . $event->id .'/scan'); // I think this should not be here, right?
return view('event.scan', compact('event','scan','booking')); // this is being ignored.
}else{
return redirect('event/' . $event->id .'/scan');
}
}
答案 1 :(得分:0)
您可以像这样更新您的方法。它更清晰易读(无论如何,我认为)。
public function store(Request $request, $id)
{
//use first to get a booking or use get() for collections of booking
$booking = Booking::where('student_id', auth()->user()->student_id)
->where('event_id', $id)
-first();
if (is_null($booking)) {
return redirect('event/' . $id . '/scan');
}
return view('event.scan', [
'event' => Event::findOrFail($id),
'scan' => Scan::create($request->all()),
'booking' => $booking
]);
}
希望这会有所帮助 干杯