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

Reundo's avatar

WhereHasMorph query doesn't work with custom morph types

I'm using different allies for morph Relations with morphMap(). When I use WhereHasMorph() and give my custom morph type, it doesn't work.

in Controller :

 $data = Product::whereHasMorph(
            'productable',
            ['PrintedBook'], // Book::class
            function (Builder $query, $type) use($title){
                $query->where('title', 'LIKE', "{$title}%");
            }
        )->get();

and in Book model :


    public function getMorphClass () {
        return $this->morphClass ?: 'Book'; // in query it uses 'Book' instead of 'PrintedBook'
    }

    public function product()
    {
        $this->morphClass = 'PrintedBook';
        return $this->morphOne('App\Models\Product', 'productable');
    }

and morphMap :

Relation::morphMap([
            'PrintedBook' => Book::class,
            // ...
        ]);

So are there any ways to solve this? Thanks.

related issue posted on github

0 likes
0 replies

Please or to participate in this conversation.