/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $this->validate($request, ['name' => 'required', 'gender' => 'required', 'phone' => 'required|numeric', 'address' => 'required']); $input = $request->only('name', 'gender', 'phone', 'address'); if (Auth::user()->role != 'user') { $guest = new Guest(); $guest->fill($input)->save(); $reservation_data = ['guest_id' => $guest->id, 'guest_type' => 'guest', 'check_in' => session('check_in'), 'check_out' => session('check_out')]; } else { $user = User::findOrFail(Auth::user()->id); $user->fill($input)->save(); $reservation_data = ['guest_id' => Auth::user()->id, 'guest_type' => 'user', 'check_in' => session('check_in'), 'check_out' => session('check_out')]; } $request->session()->forget('check_in'); $request->session()->forget('check_out'); $reservation = new Reservation(); $reservation->fill($reservation_data)->save(); $roomTypes = $request->input('room_type_id'); $roomReserved = $request->input('total_room_reserved'); for ($i = 0; $i < count($roomTypes); $i++) { $reservation->roomTypes()->attach($roomTypes[$i], ['total_room_reserved' => $roomReserved[$i]]); } return redirect('/reservation'); }