Skip to content

Morph-To-Many and MorphTo #228

Closed Answered by staudenmeir
nilskretschmer asked this question in Q&A
Discussion options

You must be logged in to vote

You can define the users() relationship from your existing relationships:

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

    public function users(): HasManyDeep
    {
        return $this->hasManyDeepFromRelations($this->roles(), (new Role())->users());
    }
}

Then use it to define the reverse relationship:

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

    public function projects(): HasManyDeep
    {
        return $this->hasManyDeepFromReverse((new Project)->users());
    }
}

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@nilskretschmer
Comment options

Comment options

You must be logged in to vote
1 reply
@nilskretschmer
Comment options

Answer selected by nilskretschmer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants