public static function saveCustomerReservationDetails($data) { //dd($data->all()); $getReferenceNumber = new Reservation(); $rooms = explode(',', $data->get('rooms')); $customer = Customer::whereEmail($data->get('email'))->first(); $total_capacity = 0; $total_person = $data->get('no_of_adult') + $data->get('no_of_child'); foreach ((array) $rooms as $rm) { $rms = Room::find($rm); $categories = Category::find($rms->category_id); if (count($categories) > 0) { $total_capacity += $categories->max_capacity; } } if ($total_capacity < $total_person) { return redirect()->back()->with('message', 'You exceeded the max capacity of the rooms'); } else { if (count($customer) == 0) { $customer = new Customer(); $customer->name = ucfirst(strtolower($data->get('first_name'))) . ' ' . ucfirst(strtolower($data->get('last_name'))); $customer->address = $data->get('address'); $customer->email = $data->get('email'); $customer->contact_number = $data->get('contact'); if ($customer->save()) { $reservation = new Reservation(); $reservation->customer_id = $customer->id; $reservation->reference_number = $getReferenceNumber->getReservationId(); $reservation->no_of_child = $data->get('no_of_child'); $reservation->no_of_adult = $data->get('no_of_adult'); $reservation->period = $data->get('period'); $reservation->status = 'RESERVED'; if ($reservation->save()) { foreach ((array) $rooms as $room) { $start_date = $data->get('start_date'); $end_date = $data->get('end_date'); # ROOM VISITOR $start_date = date("Y-m-d", strtotime($start_date)); while (strtotime($start_date) < strtotime($end_date)) { $reservation_room = new ReservationRoom(); $reservation_room->reservation_id = $reservation->id; $reservation_room->room_id = $room; $reservation_room->date_reserved = $start_date; $reservation_room->save(); $start_date = date("Y-m-d", strtotime("+1 day", strtotime($start_date))); } } } } Mail::send('emails.reservation_reference', ['customer' => $customer, 'reservation' => $reservation], function ($m) use($customer) { $m->from('*****@*****.**', 'Overlook Resort (noreply)'); $m->to($customer->email, $customer->full_name())->subject('Here\'s your RESERVATION ID'); }); return redirect()->back()->with('message', 'Reservation was successful. We sent your reservation reference in your e-mail'); } $reservation = new Reservation(); $reservation->customer_id = $customer->id; $reservation->reference_number = $reservation->getReservationId(); $reservation->no_of_child = $data->get('no_of_child'); $reservation->no_of_adult = $data->get('no_of_adult'); $reservation->period = $data->get('period'); $reservation->status = 'Not Paid'; if ($reservation->save()) { foreach ((array) $rooms as $room) { $start_date = $data->get('start_date'); $end_date = $data->get('end_date'); # ROOM VISITOR $start_date = date("Y-m-d", strtotime($start_date)); while (strtotime($start_date) < strtotime($end_date)) { $reservation_room = new ReservationRoom(); $reservation_room->reservation_id = $reservation->id; $reservation_room->room_id = $room; $reservation_room->date_reserved = $start_date; $reservation_room->save(); $start_date = date("Y-m-d", strtotime("+1 day", strtotime($start_date))); } } } Mail::send('emails.reservation_reference', ['customer' => $customer, 'reservation' => $reservation], function ($m) use($customer) { $m->from('*****@*****.**', 'Overlook Resort (noreply)'); $m->to($customer->email, $customer->full_name())->subject('Here\'s your RESERVATION ID'); }); return redirect()->back()->with('message', 'Reservation was successful. We sent your reservation reference in your e-mail'); } }