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(
            ['PrintedBook'], // Book::class
            function (Builder $query, $type) use($title){
                $query->where('title', 'LIKE', "WhereHasMorph query doesn't work with custom morph types%");

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('AppModelsProduct', 'productable');

and morphMap :

            'PrintedBook' => Book::class,
            // ...

So are there any ways to solve this?

related issue posted on github

Source: Laravel

Categorised as eloquent, laravel Tagged ,


Leave a Reply

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us