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

phegman's avatar

Static methods on Eloquent model

I am building a fantasy sports game and I have selection_periods table that keeps track of what date/time the selection period closes so users can no longer select their players. I originally had a method on the SelectionPeriod.php model like so:

    /**
     * Check if selection period is open
     *
     * @param  \Illuminate\Database\Eloquent\Builder $query
     * @param  int $week
     * @return boolean
     */
    public function scopeIsOpen($query, $week)
    {
        $selectionPeriod = $query->where('week', $week)->first();

        if ($selectionPeriod !== null) {
            return Carbon::now()->lte(Carbon::parse($selectionPeriod->closes));
        } else {
            return false;
        }
    }

But then I read in the documentation (https://laravel.com/docs/5.5/eloquent#local-scopes) that scopes should always return a query builder instance.

I like how I am able to do SelectionPeriod::isOpen() as that feels pretty expressive, but I am wondering what the best practice for doing this is? I could create a static method but I am not sure what the cleanest way to instantiate the query builder inside this method would be.

Any guidance is much appreciated!

Thanks, Pete

0 likes
5 replies
topvillas's avatar

Don't use static methods if you need the query builder.

Static methods are great for making complete queries and returning the results.

public static function isOpen($week)
{
    return (new static)::where('week', $week)->first();
}
7 likes
Caprico's avatar

Could just make is isOpen() doesn't have to be defined as a scope. That way you aren't "required" to have it return a query builder object. Your calls would also stay the same. In projects I've worked on if we want to return a boolean we use isWhatever() as the function name.

See if others have different ideas however.

1 like
phegman's avatar

Thanks @topvillas and @Caprico . Something like the below (as @topvillas suggestion) feels pretty clean!

/**
     * Check if selection period is open
     *
     * @param  int $week
     * @return boolean
     */
    public static function isOpen( $week)
    {
        $selectionPeriod = (new static)::where('week', $week)->first();

        if ($selectionPeriod !== null) {
            return Carbon::now()->lte(Carbon::parse($selectionPeriod->closes));
        } else {
            return false;
        }
    }

What would be the downsides of using a static method in an Eloquent model and what are the alternatives?

Thanks, Pete

1 like
topvillas's avatar

The downside is that (in my opinion) it doesn't really belong in the model object. It's domain logic that belongs somewhere else.

I'd recommend some sort of repository.

1 like
phegman's avatar

Okay that makes sense, I may play around with a repository pattern and see how that feels. I appreciate the help!

Please or to participate in this conversation.