/** * Run the database seeds. * * @return void */ public function run() { $sportHallId = \App\SportHall::where('name', '=', 'Hall 1')->pluck('id'); $userId = \App\User::where('name', '=', 'Jill')->pluck('id'); $bookingStatusId = \App\BookingStatus::where('status', '=', 'Reserved')->pluck('id'); DB::table('bookings')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'booking_time' => Carbon\Carbon::create(2015, 12, 10, 8, 0, 0)->toDateTimeString(), 'hours' => 1, 'sport_hall_id' => $sportHallId, 'user_id' => $userId, 'booking_status_id' => $bookingStatusId]); $sportHallId = \App\SportHall::where('name', '=', 'Hall 1')->pluck('id'); $userId = \App\User::where('name', '=', 'Jamal')->pluck('id'); $bookingStatusId = \App\BookingStatus::where('status', '=', 'Reserved')->pluck('id'); DB::table('bookings')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'booking_time' => Carbon\Carbon::create(2015, 12, 10, 10, 0, 0)->toDateTimeString(), 'hours' => 1, 'sport_hall_id' => $sportHallId, 'user_id' => $userId, 'booking_status_id' => $bookingStatusId]); $sportHallId = \App\SportHall::where('name', '=', 'Hall 3')->pluck('id'); $userId = \App\User::where('name', '=', 'Jill')->pluck('id'); $bookingStatusId = \App\BookingStatus::where('status', '=', 'Reserved')->pluck('id'); DB::table('bookings')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'booking_time' => Carbon\Carbon::create(2015, 12, 11, 8, 0, 0)->toDateTimeString(), 'hours' => 1, 'sport_hall_id' => $sportHallId, 'user_id' => $userId, 'booking_status_id' => $bookingStatusId]); $sportHallId = \App\SportHall::where('name', '=', 'Hall 4')->pluck('id'); $userId = \App\User::where('name', '=', 'Jamal')->pluck('id'); $bookingStatusId = \App\BookingStatus::where('status', '=', 'Reserved')->pluck('id'); DB::table('bookings')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'booking_time' => Carbon\Carbon::create(2015, 12, 11, 10, 0, 0)->toDateTimeString(), 'hours' => 1, 'sport_hall_id' => $sportHallId, 'user_id' => $userId, 'booking_status_id' => $bookingStatusId]); }
/** * Responds to requests to POST /bookings/edit */ public function postEdit($booking_id, Request $request) { // get the booking $booking = \App\Booking::find($booking_id); // check if booking exists if (is_null($booking)) { \Session::flash('flash_message', 'Booking not found.'); return redirect('/bookings'); } // check if the hall is booked for the given time $startTime = $request->date . 'T08:00:00.000'; $endTime = $request->date . 'T19:00:00.000'; $bookings = \App\Booking::where('booking_status_id', '=', '3')->where('id', '!=', $booking->id)->where('booking_time', '>=', $startTime)->where('booking_time', '<=', $endTime)->where('sport_hall_id', '=', $request->hall)->get(); // selected time for booking $currentBookingTime = Carbon::createFromFormat('Y-m-d H:00', $request->date . ' ' . $request->time); // hall should be booked atleast 5 hours in advance if (Carbon::now()->diffInHours($currentBookingTime, false) <= 5) { \Session::flash('flash_message', 'Booking not updated. You have to book a hall atleast 5 hours in advance. Please select an other time slot.'); return redirect('/bookings'); } // loop through all the booking on this date and check if the hall is reserved foreach ($bookings as $bookingInRange) { $startTime = Carbon::createFromFormat('Y-m-d H:00:00', $bookingInRange->booking_time); $endTime = $startTime->copy(); $endTime->addHours($bookingInRange->hours); if ($currentBookingTime->between($startTime, $endTime) && !($endTime->diffInHours($currentBookingTime) == 0)) { \Session::flash('flash_message', 'Booking not updated. Sorry the hall is already reserved for this time slot!'); return redirect('/bookings'); } } // edit the booking $booking->sport_hall_id = $request->hall; $booking->booking_time = Carbon::createFromFormat('Y-m-d H:00', $request->date . ' ' . $request->time)->toDateTimeString(); $bookingStatusId = \App\BookingStatus::where('status', '=', 'Reserved')->pluck('id'); $booking->booking_status_id = $bookingStatusId; $booking->hours = $request->hours; $booking->save(); // Done \Session::flash('flash_message', 'Your booking was modified!'); return redirect('/bookings'); }