Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in the README #45

Closed
raphaelmsr opened this issue Oct 3, 2019 · 12 comments
Closed

Error in the README #45

raphaelmsr opened this issue Oct 3, 2019 · 12 comments

Comments

@raphaelmsr
Copy link

I think there's a typo, otherwise I didn't catch the belongsToMany paragraph

Its written
public function permissions() { return $this->hasManyDeep('App\Permission', ['role_user', 'App\Role']); }

Shouldn't it be
public function permissions() { return $this->belongsToMany('App\Permission', ['role_user', 'App\Role']); }
?

@staudenmeir
Copy link
Owner

The code is correct: It shows how you can use the package to combine a BelongsToMany and a HasMany relationship into a single HasManyDeep relationship.

@raphaelmsr
Copy link
Author

So the belongsToMany is only between User and Role; the hasManyDeep is applied to the parent level ?

@staudenmeir
Copy link
Owner

Imagine your application has these models and relationships:

class User extends Model
{
    public function roles()
    {
        return $this->belongsToMany('App\Role', 'role_user');
    }
}

class Role extends Model
{
    public function permissions()
    {
        return $this->hasMany('App\Permission');
    }
}

You can use this package to define a direct relationship from User to Permission:

class User extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function permissions()
    {
        return $this->hasManyDeep('App\Permission', ['role_user', 'App\Role']);
    }
}

@raphaelmsr
Copy link
Author

Magnificent, so there is no real HasManyDeep inverse relationship in a way?
Thank you so much again

@staudenmeir
Copy link
Owner

From Permission to User? You can also use this package to define the inverse relationship.

@raphaelmsr
Copy link
Author

So the same hasManyDeep ? Similar to the inverse of belongsToMany, it is the same relationship ?

@staudenmeir
Copy link
Owner

It would look like this:

class Permission extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function users()
    {
        return $this->hasManyDeep(
            'App\User',
            ['App\Role', 'role_user'],
            ['id'],
            ['role_id']
        );
    }
}

@raphaelmsr
Copy link
Author

Even without a pivot table ? In a case where I don't have a belongstoMany in the middle

@staudenmeir
Copy link
Owner

What's your actual relationship?

@raphaelmsr
Copy link
Author

raphaelmsr commented Oct 3, 2019

Mapregion -> hasMany -> Subregion -> hasMany -> Country -> hasMany -> Group
And then Country hasMany Groups, Brands, and Units as well;
Each child belongs to its Parent; its pretty straightforward (a single subregion belongs to a single mapregion; a single brand belongs to a single group; etc)

I wanted a HasManyDeep between Mapregion and Group; Brand and Unit (using hasManyDeepFromRelations)

If i wanted to get the Mapregion of a single group; since it belongs to only one; what would be the relationship ? I use your belongsToThrough for now but I may be wrong

@staudenmeir
Copy link
Owner

In this case, a BelongsToThrough relationship is indeed the easiest solution.

@raphaelmsr
Copy link
Author

Your packages are life-saving; for real :) Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants