WhereHasMorph query doesn’t work with custom morph types

Published

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', "WhereHasMorph query doesn't work with custom morph types%");
            }
        )->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('AppModelsProduct', 'productable');
    }

and morphMap :

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

So are there any ways to solve this?
Thanks.

related issue posted on github

Source: Laravel

Published
Categorised as eloquent, laravel Tagged ,

Answers

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
faq