I have a projects table that either can be assigned to a user or a team, this is the structure:
id
assignable_id
assignable_type
class Project extends Model {
public function assignable(): MorphTo
{
return $this->morphTo();
}
}
and my team table contains members with a pivot table:
id
class Team extends Model {
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
public function projects(): MorphMany
{
return $this->morphMany(Project::class, 'assignable');
}
}
// team_user table
team_id
user_id
and finally my user table:
id
class User extends Model {
public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class);
}
public function projects(): MorphMany
{
return $this->morphMany(Project::class, 'assignable');
}
}
Now the problem is I want to get all projects that have been assigned to a user either directly or through a team, as expected $user()->projects return the ones that directly have been assigned to the user.
I tried many solutions but none has worked for me yet.