Dynamic parameter in global scope?
I have a controller method that uses a local scope to check if we have event data in our db for today's date (or an optional route parameter of $date), if we do not, I perform an api request to get the data for today and store it.
eg: /station/{station} or /station/{station}/{date}
public function show(Station $station, $date = null)
{
if (!$station->events()->today($date)->exists()) {
// perform api call etc...
}
return view('stations.show', compact('station'));
}
I also have a global scope on the Events model to select only dates greater than equal to today to show in the view (since we don't want to show older data):
protected static function boot()
{
parent::boot();
static::addGlobalScope('date_time', function (Builder $builder) {
$builder->whereDate('date_time', '>=', Carbon::today()->toDateString());
});
}
However, if the optional $date parameter is available, I would like the global scope to be greater than or equal to that date.
Is this possible so that I can continue with route model binding? Thank you.
Please or to participate in this conversation.