phplaraveleloquent

Eloquent trying to get date with time


I'm trying to get all the reservations in a specific date and time and count the guests to find the available seats

What i'm doing in the controller:

public function getSeats() {
    
    $data = request()->validate([
        'date' => 'required',
        'hours' => 'required',
        'place_id' => 'required'
    ]);

    $hours = [];

    foreach($data['hours'] as $h) {

        $date = $data['date'].' '.$h;

        //Carbon date: 2021-08-31 08:00:00 | Database Date: 2021-08-31 08:00:00

        $count = Reservation::where('place_id', $data['place_id'])->whereDate('date', Carbon::create($date)->toDateTimeString())->sum('guests');

        $object = (object) [
            'hour' => $h,
            'guests' => $count
        ];


        array_push($hours, $object);
    }

}

It returns null, what am i doing wrong?

**Edit I'm also using 24H in the time selector, so when i create a reservation at 12:00 in the morning eloquent grabs it as 00:00 in the night.


Solution

  • Using where instead of whereDate fixed the issue