Skip to content
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

ClassMorphViolationException exception with model:show artisan command #43216

Closed
SamuelMwangiW opened this issue Jul 16, 2022 · 2 comments
Closed

Comments

@SamuelMwangiW
Copy link
Contributor

  • Laravel Version: 9.x-dev
  • PHP Version: 8.1.7
  • Database Driver & Version: Mariadb 10.7

Description:

Thanks a lot for the amazing work @jessarcher on #43156 .

Am getting an Illuminate\Database\ClassMorphViolationException because I have Relation::enforceMorphMap for some classes. Kindly confirm if this is the desired behavior
image

Relation::enforceMorphMap([
    'post' => Post::class,
    'video' => Video::class,
]);

It otherwise works perfectly fine when the enforceMorphMap is commented out:
image

Steps To Reproduce:

  • Add Relation::enforceMorphMap(['model'=>'App\Models\Model']) in the boot method of AppServiceProvider and omitting to add \App\Models\User
  • Run artisan model:show User
@jessarcher
Copy link
Member

Hi @SamuelMwangiW, I think this is expected. The tokens and notifications relationships on your User model are both MorphMany relationships, so you'd need to specify a morph map for them to work in this scenario. You would get the same exception if you tried to access those relations anywhere else in your application.

If you're not using these relationships, you may consider removing the HasApiTokens and Notifiable traits from the model.

I've confirmed that the exception is not thrown if an "unmapped" model doesn't have any polymorphic relations, so I wouldn't consider this a bug.

@SamuelMwangiW
Copy link
Contributor Author

Thanks @jessarcher

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants