Your are in the same namespace :
return $this->morphMany('Book','bookable');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi guys, I'm working with polymorphic relations and I found the $moprhClass Property to be useful to short the name of the class when stored on the DB.
example: Acme\Book -> Book
Inserting the data it works and it insert the short name as expected. But When I need to fetch the polymorphic data with an eager loading relation, It try to find the class stored as the short name of it, for this reason an Class Not Found Exception is thrown.
Example:
namespace Acme;
class Book extends Eloquent {
protected $morphClass = 'Book';
public function bookable() {
return $this->morphTo();
}
}
// Another file
namespace Acme\User;
class User extends Eloquent {
public function books() {
return $this->morphMany('Acme\Book','bookable');
}
}
Getting result
User::with('books')->get();
This throw the exception:
Class Book not Found Exception
What's the benefit to use $morphClass Property then?
Please or to participate in this conversation.