New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Relation::enforceMorphMap causes error when attaching resource via pivot #3547
Comments
Belongs to many doesn't use morph mapping. Please provide correct example on a new issue so we can follow/replicate the issue properly. |
@crynobone That wasn't the issue raised |
To solve this you need to define |
@crynobone Appreciate the suggestion and it would certainly work, however, we would have to create these useless model classes specifically for that purpose. The issue there is that Laravel docs now has no mention of the old The only fix would be some nova method to handle this which catches the This workaround does work or disabling action events. Relation::enforceMorphMap([
MorphPivot::class => MorphPivot::class,
Pivot::class => Pivot::class,
]); |
The old method has NOT been deprecated. |
A simple solution in nova could be before the getMorphClass is called, the enforce state is saved in a temporary variable, then disabled and set back to the temporary variable. This could also be added as a method to |
This would cause an invalid state where on one side you wish to enforce morph map but on the other side, Laravel Nova hide/ignore this completely.
This works fine on Laravel Nova Dusk Suite application |
👋 Hi there! In order to keep this repository focused on bug reports, we auto-close feature requests and requests for help. Feel free to post your feature requests so others can discuss and add reactions. We'll keep an eye on them for later planning. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
TL;DR
Getting this exception on a
belongsToMany
relation via pivot after switching toRelation::enforceMorphMap
as there isn't a mapping forIlluminate\Database\Eloquent\Relations\Pivot
?!Description:
Laravel PR of change merged: laravel/framework#38656
Laravel version released in 8.59.0
Docs were changed to suggest only using this method:
https://laravel.com/docs/8.x/eloquent-relationships#custom-polymorphic-types
If you use the old
Relation::morphMap
this works fine still, however, to be strict about morph types it seems that it is now recommended to switch toRelation::enforceMorphMap
Detailed steps to reproduce the issue on a fresh Nova installation:
See the example structure and code below taken from laravel docs:
https://laravel.com/docs/8.x/eloquent-relationships#many-to-many
If you try and attach a role to the user in nova with this setup you will get the above error.
Structure
Model
AppServiceProvider@register - Relation::enforceMorphMap:
App/Nova/UserResource@fields
Workaround
Add pivot classes to morph map:
Note: I believe that a polymorphic pivot will have the same error with resolving the
MorphPivot
class and will need to have the same fix as this error.Conclusion
It seems that the
ActionEvent::forAttachedResource
is gettingIlluminate\Database\Eloquent\Relations\Pivot
as the$pivot
rather than a model and callsgetMorphClass()
on it which errors as it doesn't exist in the enforced morph map.The text was updated successfully, but these errors were encountered: