-
Notifications
You must be signed in to change notification settings - Fork 75
Scopes not resolved on relation chains #646
Copy link
Copy link
Open
Labels
Description
Problem
When calling a model scope through a relation chain, the plugin does not resolve it.
class Post extends Model
{
public function scopePublished(Builder $query): Builder
{
return $query->where('published', true);
}
}
class User extends Model
{
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
}
// ❌ UndefinedMagicMethod — "published" not found
$user->posts()->published()->get();
// ✅ These work (handled by MethodForwardingHandler)
$user->posts()->where('published', true)->get();
$user->posts()->orderBy('created_at')->get();Cause
MethodForwardingHandler searches EloquentBuilder and QueryBuilder for forwarded methods, but does not check the related model for scope{Name}() methods or #[Scope] attributes.
Fix
When resolving a forwarded method on a Relation:
- Extract
TRelatedModelfrom the relation's generic params - Check the related model's class storage for
scope{MethodName}()or#[Scope]attribute methods - If found, return the relation type (self-returning / fluent)
Related
- Plugin handlers don't fire for proxied method calls (
__call,__callStatic, facades) #591 — broader proxy/forwarding problem - Static query builder methods and local scopes not resolved on Model classes #498 — scopes on Model static calls (closed)
Reactions are currently unavailable