Writing Reservation Code (API)

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]); } 
+8
php mysql laravel
source share
1 answer

We can use the database features to filter available cars in the time interval. Using the query builder, we can search for every car that does not have a reservation within the specified window.

In Car

 /** * @param $from Carbon * @param $to Carbon * @return \Illuminate\Database\Eloquent\Collection */ public static function whereAvailableBetween($from, $to) { $rows = DB::table('car') ->leftJoin('car_reservation', 'car_reservation.car_id', '=', 'car.id') ->leftJoin('reservation', function($join) use ($from, $to) { return $join->on('reservation.id', '=', 'car_reservation.reservation.id') ->where('reservation.date_start', '>=', $from->toDateTimeString()) ->where('reservation.date_end', '<=', $to->toDateTimeString()); }) ->whereNull('reservation.id') ->get(); return $rows->map(function($r, $k) { return new static($r); }); } 

Now we can use \Car::whereAvailableBetween($date_start, $date_end) . To return it as JSON from your controller, you can:

 public function get(Request $request) { return Car::whereAvailableBetween(Carbon::parse($request->from), Carbon::parse($request->to)); } 

EDIT

I missed that desired end format

['Car' => true, 'Car' => false]

Thus, you can change the method described above and return the standard collection with this data by deleting the whereNull :

 /** * @param $from Carbon * @param $to Carbon * @return \Illuminate\Support\Collection */ public static function whereAvailableBetween($from, $to) { $rows = DB::table('car') ->leftJoin('car_reservation', 'car_reservation.car_id', '=', 'car.id') ->leftJoin('reservation', function($join) use ($from, $to) { return $join->on('reservation.id', '=', 'car_reservation.reservation.id') ->where('reservation.date_start', '>=', $from->toDateTimeString()) ->where('reservation.date_end', '<=', $to->toDateTimeString()); }) ->select('car.*', 'reservation.id AS reservation_id') //->whereNull('reservation.id') if it is null then no reservation, else there is a reservation ->get(); return $rows->map(function($row, $k) { $asArray = (array)$row; $reservation_id = array_pop($asArray); $available = (is_null($reservation_id)) ? true: false; return [ 'car' => new static($asArray), 'available' => $available ]; }); } 

Keep in mind that you can also add an attribute to the model. So we could do $car->available = true

+2
source share

All Articles