I am creating a CMS package for Laravel .
All my models in this package are bound and enabled from the IoC container so that they can easily be overwritten in any single package deployment.
For non-polymorphic relationships, this worked.
For example, Page has many PageModules , so its relationship has changed from:
// \Angel\Core\Page public function modules() { return $this->hasMany('PageModule'); }
in
// \Angel\Core\Page public function modules() { return $this->hasMany(App::make('PageModule')); }
But I was not able to figure out how to do the same with polymorphic relationships.
For example, menus contain MenuItems , and each MenuItem can be tied to one other model, such as a page or BlogPost.
To achieve this Laravel path, I added the following to MenuItem:
// \Angel\Core\MenuItem public function linkable() { return $this->morphTo(); }
And this is the relation to LinkableModel , which applies to all models, such as Page and BlogPost:
// \Angel\Core\LinkableModel public function menuItem() { return $this->morphOne(App::make('MenuItem'), 'linkable'); }
And the menus_items table (which MenuItems use) has the following rows:
linkable_type | linkable_id -------------------|-------------- \Angel\Core\Page | 11 \Angel\Core\Page | 4
This works fine, but I need linkable_type say βPageβ instead of β\ Angel \ Core \ Pageβ and be allowed on the βIoCβ page instead of being hardcoded for a particular class namespace.
What I tried:
According to this question , this should be as simple as defining the $morphClass property for linkable () classes, for example:
// \Angel\Core\Page protected $morphClass = 'Page';
But when I apply this and modify the menus_items table to look like this:
linkable_type | linkable_id ---------------|-------------- Page | 11 Page | 4
... I just get a Class 'Page' not found. error Class 'Page' not found. whenever linkable () is called in MenuItem.
This is the exact line in Eloquent that throws an error.
So, I burst into Eloquent and thought I could get away with something like this:
// \Angel\Core\MenuItem public function linkable() { return $this->morphTo(null, App::make($this->linkable_type)); }
... this is so close, but alas: Eloquent calls linkable () before filling in the remaining attributes / columns of MenuItem, so $ this-> linkable_type is null and therefore cannot solve anything from IoC.
Thank you so much for any guidance you may have!