Multiple "types" on same model on MorphMap polymorphic relationship
Hi all, I need to map multiple "types" pointing to same model over a polymorphic relationship. This because in my application i set a laravel-permission authentication package that fails if i don't include his model type in AppServiceProvider.php boot method.
I've got a Resource model that has a polymorphic relationship with User model and Equipment model. Every User or Equipment has many related Resources, so:
// User.php Model class
...
public function resources(): MorphMany {
return $this->morphMany(Resource::class, 'resourceable', 'resourceable_type','resourceable_id','id');
}
...
// Equipment.php Model class
...
public function resources(): MorphMany {
return $this->morphMany(Resource::class, 'resourceable', 'resourceable_type','resourceable_id','id');
}
...
// Resource.php Model class
...
public function resourceable(): MorphTo {
return $this->morphTo('resource', 'resourceable_type','resourceable_id', 'id');
}
...
To map the relationship "types" i add a MorphMap method to AppServiceProvider.php, but i must map the laravel-permission type too (auth not works otherwise):
// AppServiceProvider.php
...
public function boot() {
Relation::morphMap([
'App\Model\EloquentUser' => User::class,
'staff' => User::class,
'rent' => Equipment::class,
]);
}
...
So:
- if I map laravel-permission type and also a custom "staff" type, Auth works, but model User not loads his Resources.
- if I map only "staff" and "rent", authentication not works.
- if I invert laravel-permission and "staff" types, auth not works, but User loads his Resources.
- if I use 'App\Model\EloquentUser' type on Resource table (like laravel-permission), auth and relationship loading both works (but i need the custom "staff" type).
It seems that MorphMap maps only a single type for every model, but i find some people that successfully uses multiple types on same model. Where am I wrong? Who can help me?
Please or to participate in this conversation.