You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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]);
}
}
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
andusers
. 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
andemployer_id
) that we want to take into account to get the correspondingjobs
. Whenemployer_id
is set on theuser
I also want to include thejobs
that are associated with theemployer
.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.
The text was updated successfully, but these errors were encountered: