mercredi 20 juin 2018

Setting a value if a condition is fulfilled inside a where condition

I'm trying the following:

I have a function that returns pubs only if they are open in current moment,

/**
 * @param $pubs
 * @return mixed
 */
public static function isPubCurrentlyOpen($pubs)
{
    $pubs->whereHas('pubSchedules', function ($pubs) {
        $pubs->where('week_day', Carbon::now()->dayOfWeek)
            ->whereRaw(
                "'" . Carbon::now('Europe/Madrid')->format("H:i:s") . "' BETWEEN opening_time AND closing_time"
            );
    } );

    return $pubs;
}

The problem I have is when some pubs have a closing_time after 00:00 (it would be "next day"), so if I use this function, i.e. 18:00:00, it wont return those pubs.

So, is there any way to make this (setting one more day to closing_time) inside the query statement I wrote before?:

if($openingTime>$closingTime)
{
  $closingTime = Carbon::now('Europe/Madrid')->setTimeFromTimeString($schedule->closing_time)->addDay(1);
} 

Thanks a lot!!

Aucun commentaire:

Enregistrer un commentaire