Nov 25, 2021
0
Level 1
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.
Please or to participate in this conversation.