Laravel-modules: How to use models from other Modules?

Created on 23 Aug 2016  路  17Comments  路  Source: nWidart/laravel-modules

Say I have a User module.
And say I also have a Blog module.

The Blog module has a Post model/entity.

I want to use the User entity from the User module in a posts() relationship to the Post entity from the Blog module.

I.e. $user->posts()

In Ruby, this was possible by way of class_eval, which allows you to re-open an existing class and add methods to it. E.g., in this case we could've re-opened the User entity (which is from a different module) from within the Blog module and added a public function posts() method.

How can we do this in PHP and Laravel?

Most helpful comment

No, this _is_ a question related to this package. You probably think I'm talking about hasOne/hasMany relationships in Laravel, but no I'm not.

I'm asking: how do you make entities from one module usable by entities from another module without hard-coding the dependencies? Do I have to create a 3rd module to connect the two modules?

For example, I don't see a user_id field on a Post in the AsgardCMS Blog module. How would you connect the User and the Blog module such that a user_id is available in the Blog module without hardcoding the Post entity into the User module?

All 17 comments

Hi,

This isn't a question related to this package. Please look up the laravel documentation to learn how you can do this. You could also go to the laracasts.com and view the videos.

No, this _is_ a question related to this package. You probably think I'm talking about hasOne/hasMany relationships in Laravel, but no I'm not.

I'm asking: how do you make entities from one module usable by entities from another module without hard-coding the dependencies? Do I have to create a 3rd module to connect the two modules?

For example, I don't see a user_id field on a Post in the AsgardCMS Blog module. How would you connect the User and the Blog module such that a user_id is available in the Blog module without hardcoding the Post entity into the User module?

You would do it the same way, by using the namespace of the second module in your first module.

// in module User
public function posts()
{
    return $this->hasMany(Modules\Blog\Models\Posts::class);
}

If you want another way to avoid dependencies that would be up to you to find your preferred approach.

I don't understand why you mention AsgardCMS here, if it's issue on that you should fill issue there.

You can do relations however you like there are no constraints, this one just separates resources into module like structure, so it's much more easier to maintain without clutter in 1-2 folders.
Example provided by @nWidart is a standart laravel way of doing relation.

I mentioned AsgardCMS to give an example because the maintainer of this package is the creator of the CMS.

This issue does relate to this package . The issue is how can we create relationships between modules without hard-coding the dependencies? If someone can provide a good answer, it would help many people using this package to create even more modular systems.

Okay, i see what you want.
You can look at this example: AsgardCMS/Page
It's dynamically loaded into entity.
And you can define bunch of them in configs: relation-config

But it's implementation of idea, not really this package specific.

That's great, thanks!

@armababy Thanks it's the first question popped to my mind when found this library, was suprised its not mentioned anywhere

Because it's not this package role to do this. I handled it that way in asgardcms, but you can just as well find a better way to do it altogether. 馃槃

As another way: https://github.com/greabock/tentacles
Maybe this is not the best way, but there is something to think about.
;)

@n4p4 @nWidart Can't we extend User _Model_ in Post _Modules_ and then add a relationship in the extended User Model?

You can yes, this is just php indeed.

Using a closure in the config's will break artisan config:cache. Is there a way to __call without passing back a closure from the config?

when i am seeding , newly created modules seeder files are not seed ,

Are you sure, you are using the right command, @kathmandu1
php artisan module:seed Module ?

Otherwise, you can always try with composer dump-autoload, you know just in case.

Hi I stole this answer from Issue 345:

You can define the requirement in the module.json file at the root of the module.

That's how I am probably going to proceed.

Here is your solution please check this link
https://github.com/imanghafoori1/eloquent-relativity

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FoksVHox picture FoksVHox  路  11Comments

Warchiefs picture Warchiefs  路  12Comments

themodernpk picture themodernpk  路  10Comments

oleaass picture oleaass  路  11Comments

esipavicius picture esipavicius  路  13Comments