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
I'm trying to make a relation between my 2 MongoDB models. But instead of using the default way of pushing ID's to an array I want to use a Pivot Collection.
I could do this in the following way:
final class Project extends Model
{
public function projectEmployeePivots(): HasMany
{
return $this->hasMany(ProjectEmployeePivot::class);
}
public function employees(): Collection
{
return $this->projectEmployeePivots()
->with('employee')
->get()
->pluck('employee');
}
}
But I would prefer to return a Relation instead of a Collection, so I can query more efficiently on the relation, without fetching all of the Related Employees.
Here is what I have so far, but this is not working because I'm calling ->employees() on a Builder instance.
final class Project extends Model
{
public function employees(): hasMany
{
$relation = $this->hasMany(Employee::class);
$relation->setQuery(
ProjectEmployeePivot::with('employee')->employees()->getQuery()
);
return $relation;
}
}
So I'm a bit lost now how I should try to setup this relation, so I'm asking here. Thanks in advance!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone,
I'm trying to make a relation between my 2 MongoDB models. But instead of using the default way of pushing ID's to an array I want to use a Pivot Collection.
I could do this in the following way:
But I would prefer to return a Relation instead of a Collection, so I can query more efficiently on the relation, without fetching all of the Related Employees.
Here is what I have so far, but this is not working because I'm calling ->employees() on a Builder instance.
So I'm a bit lost now how I should try to setup this relation, so I'm asking here. Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions