public function followedProjects(): BelongsToMany
{
return $this->belongsToMany(Project::class, 'project_follower', 'follower_id', 'project_id');
}
public function assignedProjects(): BelongsToMany
{
return $this->belongsToMany(Project::class, 'project_designer', 'designer_id', 'project_id');
}
Migration to create view
use Staudenmeir\LaravelMergedRelations\Facades\Schema;
Schema::createMergeView(
'followers',
[
(new User)->followedProjects()->withPivot('sorting'),
]
);
Schema::createMergeView(
'designers',
[
(new User)->assignedProjects()->withPivot('sorting')
]
);
Schema::createMergeView(
'all',
[
(new User)->followedProjects()->withPivot('sorting'),
(new User)->assignedProjects()->withPivot('sorting')
]
);
The view followers has a column: __project_follower__pivot__sorting.
The view designers has a column: __project_designer__pivot__sorting.
But the view all doesn't have any extra columns.
It's probably because the names of the columns in followers and designers doesn't match.
If this is the case, maybe give the columns an alias to use in the field?
I'm unable to use withPivot() in mergeRelation when use two different pivot tables.
Code to replicate:
Using pivot-tables:
App\Models\User.php
Migration to create view
The view
followers
has a column:__project_follower__pivot__sorting
. The viewdesigners
has a column:__project_designer__pivot__sorting
.But the view
all
doesn't have any extra columns. It's probably because the names of the columns infollowers
anddesigners
doesn't match.If this is the case, maybe give the columns an alias to use in the field?