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

Include Records By Multiple Parent Keys #67

Open
MannikJ opened this issue Nov 25, 2019 · 2 comments
Open

Include Records By Multiple Parent Keys #67

MannikJ opened this issue Nov 25, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@MannikJ
Copy link

MannikJ commented Nov 25, 2019

First of all thank you for this great package. I have thankfully used it several times in the past.

Now I have a related use case this package does not solve: Instead of multiple foreign keys, there are multiple parent keys I want to include.

In our case there are jobs and users. Each user can have multiple jobs. And a user can also belong another user via an employer/employee relationship.

There are two columns on the parent users table (id and employer_id) that we want to take into account to get the corresponding jobs. When employer_id is set on the user I also want to include the jobs that are associated with the employer.

I know this package is actually about something else, but as it's quite hard to understand how constraints are applied etc., I thought it would make sense to ask in place where people have been dealing with very similar stuff already.
Of course the biggest problem is to make it work with eager loading as well.

@topclaudy
Copy link
Owner

This could be an interesting feature to have in Compoships.

@Namoshek
Copy link

I haven't used this myself, but you could use two relationships and an accessor which provides combined data. You could also use a query scope for convenience, if you want to add multiple relationships to eager load.

class User
{
    public function supervisor()
    {
        return $this->belongsTo(User::class, 'supervisor_id', 'id');
    }

    public function userPosts()
    {
        return $this->hasMany(Post::class, 'user_id', 'id');
    }

    public function supervisorPosts()
    {
        return $this->hasMany(Post::class, 'supervisor_id', 'id');
    }

    public function getPostsAttribute()
    {
        return $this->userPosts->concat($this->supervisorPosts);
    }

    public function scopeEagerLoadPosts($query)
    {
        $query->with(['userPosts', 'supervisorPosts']);
    }
}

You can then use it like this:

class UserController
{
    public function show($id)
    {
        $user = User::eagerLoadPosts()->find($id);

        $posts = $user->posts; // contains posts of user and supervisor

        return view('user.show', ['user' => $user, 'posts' => $posts]);
    }
}

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

No branches or pull requests

3 participants