skip to Main Content

App For Rental Car :
I need check car if available between 2 date and check quantity ("column name : qty"),
There is no need for a car more than the available number of cars (number cars booking < QTY car )
When change number 3 in the name of the column (‘qty’) does not work.
Query From Url : $PickupStartDate,$DropEndDate;

Car::withCount('reservation')->having('reservation_count', '<', 3)->get();

Tables :

Cars
ID | name | price | qty|

Reservations
ID | start_date | end_date | days | start_place | return_place | car_id |

check if car available for rental check we have a car between date

2

Answers


  1. Chosen as BEST ANSWER

    Solution :

        $start_date= $filters['StartDate'];
        $end_date= $filters['EndDate'];
        $query->withCount(['reservation' => function($query) use ($start_date, $end_date) {
            $query->where('start_date', '<=', $start_date)
            ->where('end_date', '>=', $end_date)
            ->orWhere('end_date', '>', $start_date);
        }])
    

    in code Check Car.qty< car.reservation_count


  2. $start_date = $PickupStartDate;
    $end_date = $DropEndDate;
    
    $cars = Car::with(['reservations' => function($query) use ($start_date, $end_date) {
        $query->where('start_date', '<=', $end_date)
              ->where('end_date', '>=', $start_date);
    }])->having(3, '<', 'reservations_count')->get();
    

    Try this

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search