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();
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
Please or to participate in this conversation.