5 months ago

Should a scope return the builder?

Posted 5 months ago by roggel

The Laravel documentation clearly states that local scopes should return the Builder. However, if you don't return it, the scope still works and remains chainable.

public function scopeEnabled(Builder $query)
    $query->where('enabled', true); // not returning the query

I tried to understand why this still works (i.e. User::enabled()->where('created_at', '<', '2018')->get() works like a charm), but I can't quite wrap my head around it.

Are there any drawbacks to not returning the builder instance? Situations where it would not work properly?

Please sign in or create an account to participate in this conversation.