Why not just
class User extends Eloquent{
public function pages(){ return $this->hasMany('Page'); }
}
Why do you want to make it dynamic ?
I want to do it dynamically, becouse I want to build an modular application.
For example I have some modules (Projects, Pages, Photos, Contacts, Calendars etc.) that I may install and uninstall, I don't want to change the User model code for each module with:
public function pages() { return $this->hasMany('Page'); }
public function projects() { return $this->hasMany('Project'); }
public function photos() { return $this->hasMany('Photo'); }
public function contacts() { return $this->hasMany('Contact'); }
public function calendars() { return $this->hasMany('Calendar'); }
// ...
But I want to use:
Auth::user()->pages;
Auth::user()->projects;
// ...
The dynamic way like in my first post seems to be the only way or is there another way to do this?
You may rethink how your application is structured. I can't see why there is a problem about specifying every relationships and use them when you need to.
I can't specify every relationship in the User model. This is not possible, becouse I can't know what relations would come with the installation of other modules.
I don't know if you are familiar with TYPO3, but this is a good example to explain my problem. TYPO3 has an extension manager to install extensions that are build by 1000s of different developers. Something like that I want to build with Laravel.
So some of these extensions my extend the users-table. You would add the relationships of all the extensions, that could be found on the extension manager in the User-model of the core application? Really? I don't think, that this is a good idea. I think, that if the extensions can extend the User-model like my first post shows would be a better way.
You would probably want to use a polymorphic relationship for this. You might be able to accomplish this if you define an extension as a polymorphic relationship, but it still seems pretty complex.
Maybe you should investigate the source code of TYPO3 and see how they manage extensions? They may have come up with a better solution.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community