Search code examples
phpmysqllaraveleloquentlaravel-collection

condition for two different 'get' collection inside foreach loop in laravel blade


I have two get collections for - 1) availabletime and 2) bookingtime.

For available time -

$availabletimes= AvailableTime::where('available_day_id', $availabledayid)
                               ->get(['available_time']);

Example Output:

(3) [
     {available_time: "10:00:00"},
     {available_time: "12:00:00"},
     {available_time: "13:00:00"}
    ]

For booking time -

$bookingtimes = Appointment::where('specialist_id', $specialistid)
                           ->where('booking_date', '=', $date)
                           ->get(['booking_time']);

Example Output:

(2) [
     {booking_time: "10:00:00"},
     {booking_time: "13:00:00"}
    ]

I want to show all the available times in the blade file and check if appointment_time and booking_time are the same.

Inside blade file, I am trying like-

@foreach ($availabletimes as $index => $element)    
<p>{{ $element->available_time }} - 
   {{--here I want to check if booking time is equal to available time--}}</p>
@endforeach

Solution

  • use pluck to get time in array

    $bookingtimes = Appointment::where('specialist_id', $specialistid)
                               ->where('booking_date', '=', $date)
                               ->pluck('booking_time')->toArray();
    

    By this you'll get array of the times.

    [0=>"10:00:00",1=>"13:00:00"];
    

    Now you can use in_array() in foreach

    @foreach ($availabletimes as $index => $element)    
    <p>{{ $element->available_time }} - 
       @if(in_array($element->available_time,$bookingtimes)) 'time is available' @endif</p>
    @endforeach