Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skinnyvin's avatar

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.

0 likes
2 replies
skinnyvin's avatar

Thank you @joyner

I've switched to another local scope for now. That does give food for thought though, thanks.

Please or to participate in this conversation.