I made most of the application I'm working on, and now I think I'm stuck in one change. I have ideas to do this, but a problem that I really could not implement. I hope I can help.
I have this complicated code. It takes two dates and checks the car_reservation pivot tables for overlapping.
$table->integer('car_id')->unsigned(); $table->foreign('car_id')->references('id')->on('cars'); $table->integer('reservation_id')->unsigned(); $table->foreign('reservation_id')->references('id') >on('reservations');
Relationships are in the reservation model:
public function cars() { return $this->belongsTo('App\Models\Access\Car','car_reservation'); }
Here is the code I'm trying to debug and make it work:
public function get(Request $request) { $appointments = Reservation::with('cars')->get(); foreach ($appointments as $appointment) { $from = Carbon::parse($request->from); $to = Carbon::parse($request->to); $eventStart = Carbon::instance(new DateTime($appointment['dt_start'])); $eventEnd = Carbon::instance(new DateTime($appointment['dt_end']))->subSecond(1); // A spot is taken if either the from or to date is between eventStart and eventEnd // or if the evenStart and eventEnd are between the from and to date. if ($from->between($eventStart, $eventEnd) || $to->between($eventStart, $eventEnd) || ($eventStart->between($from, $to) && $eventEnd->between($from, $to))) { return response()->json('false');// test response } return response()->json('no appointments overlapping'); } }
But I need help writing these steps, and I think this will work just fine.
(1) A method for obtaining appoitmenets from car_reservation within an additional date range. Example: getAppointments($from=null,$to=null
(2) loop method of all machines and arrange them in an array. Example: getCars
(3) Availability check method. Example: isSlotAvailable($from,$to,$appoitments);
(4) The method that does the work:
function getAvailability(Request $request) { $slots = []; $from = $request->input('from'); $to = $request->input('to'); foreach ($this->getcars() as $cars) { $appointments = $this->getAppointments($cars, $from, $to); $slot[$cars] = $this->isSlotAvailable($from, $to, $appointments); } return $slots; }
Then in the end I hope to get something like ['Car' => true, 'Car' => false]
You will help you appreciate. I came up with a lot of codes, but they all look like my original ones.
UPDATE
public static function findAppointmentsBetweenDates($start, $end) { $appointments = Reservation::wherenotBetween('from_date',array($start,$end))->get(); return $appointments; }
then in my controller
public function get(Request $request) { $results = array(); $car = Car::doesntHave('reservations')->get(); if (!$car->isEmpty()) { $results[] = $car; return Response::json(['array'],$results); } $from = Carbon::parse($request->from)->addHour(2); $to = Carbon::parse($request->to); $appointmentsBetweenDates = Reservation::findAppointmentsBetweenDates($from, $to); foreach ($appointmentsBetweenDates as $appointment) { $results = ($appointment->cars); } return Response::json(['array',$results]); }