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

aarontharker's avatar

Declaration of 'scope' must be compatible with 'parent_mode_scope'

I have a model (App\Models\Post) that extends a model from a composer package (LaraZeus\Sky\Models\Post). I'm trying to adjust one of the built-in scopes, so I thought I just needed to redeclare the method in my extension class but when I do I get this error PHP Fatal error: Declaration of App\Models\Post::scopeRelated(Illuminate\Database\Eloquent\Builder $query, App\Models\Post $post): Illuminate\Database\Eloquent\Builder must be compatible with LaraZeus\Sky\Models\Post::scopeRelated(Illuminate\Database\Eloquent\Builder $query, LaraZeus\Sky\Models\Post $post): Illuminate\Database\Eloquent\Builder in /home/aaron/nftnews/app/Models/Post.php on line 118

I just copied and pasted the original scope from the parent model and added a second where clause. so this

     * @param  Builder<Post>  $query
     */
    public function scopeRelated(Builder $query, Post $post): Builder
    {
        return $query->where('post_type', 'post')
            ->withAnyTags($post->tags->pluck('name')->toArray(), 'category');
    }

turned into this

     * @param  Builder<Post>  $query
     */
    public function scopeRelated(Builder $query, Post $post): Builder
    {
        return $query->where('post_type', 'post')
			->where('create_at', '>', now()-<subyear())
            ->withAnyTags($post->tags->pluck('name')->toArray(), 'category');
    }

Can anyone tell me why this doesn't work?

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

You have a different Post class:

// Your implementation accepts App\Models\Post
scopeRelated(Builder $query, App\Models\Post $post): Builder

// The base class accepts LaraZeus\Sky\Models\Post
scopeRelated(Builder $query, LaraZeus\Sky\Models\Post $post): Builder

Change your implementation to match the parent class, e.g.

public function scopeRelated(Builder $query, \LaraZeus\Sky\Models\Post $post): Builder
{
    return $query->where('post_type', 'post')
        ->where('create_at', '>', now()-<subyear())
        ->withAnyTags($post->tags->pluck('name')->toArray(), 'category');
}
1 like

Please or to participate in this conversation.