F
Filament16mo ago
Abi

No morph map defined for model

I get the following error on the edit page of the User Resource on the admin panel and not sure what may be the issue
No morph map defined for model [App\Models\User].
No morph map defined for model [App\Models\User].
My form method has the following code
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->disabled()
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->required()
->disabled()
->maxLength(255),
Forms\Components\TextInput::make('additionalDetail.user_type')
->label('User Type'),

Forms\Components\Fieldset::make('Address')
->relationship('additionalDetail')
->disabled()
->schema([
Forms\Components\TextInput::make('phone'),
Forms\Components\TextInput::make('city')
->label('City')
->maxLength(255),
Forms\Components\TextInput::make('user_type')
->label('User Type')
->maxLength(255),
]),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->disabled()
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->required()
->disabled()
->maxLength(255),
Forms\Components\TextInput::make('additionalDetail.user_type')
->label('User Type'),

Forms\Components\Fieldset::make('Address')
->relationship('additionalDetail')
->disabled()
->schema([
Forms\Components\TextInput::make('phone'),
Forms\Components\TextInput::make('city')
->label('City')
->maxLength(255),
Forms\Components\TextInput::make('user_type')
->label('User Type')
->maxLength(255),
]),
]);
}
and the relation on the User model is as follows:
public function additionalDetail(): HasOne
{
return $this->hasOne(UserAdditionalDetail::class, 'user_id', 'id');
}
public function additionalDetail(): HasOne
{
return $this->hasOne(UserAdditionalDetail::class, 'user_id', 'id');
}
User model has the following additions from the default
class User extends Authenticatable implements MustVerifyEmail, FilamentUser
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
use HasRoles;

protected $guard_name = 'web';
class User extends Authenticatable implements MustVerifyEmail, FilamentUser
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
use HasRoles;

protected $guard_name = 'web';
15 Replies
Abi
AbiOP16mo ago
the same code works with Filament v2, not sure what the issue is
cheesegrits
cheesegrits16mo ago
I get that from LW3. Seems like if you use morph maps for any models, you have to add them for all models LW3 interacts with, even if you don't have any polymorphic relations to them. Don't know if that's a feature or a bug, just something I found when migrating one of my apps to v3.
Abi
AbiOP16mo ago
could you elaborate what the fix is actually? i get this error only on the filament admin panel
cheesegrits
cheesegrits16mo ago
Do you use morph maps at all?
Abi
AbiOP16mo ago
ya, am not sure what exactly to do to fix this issue
cheesegrits
cheesegrits16mo ago
Try just adding your User model to your morph map. I had to add my User model to mine, even tho I have no polymorphic relations that use it.
Relation::enforceMorphMap([
'vendor' => \App\Models\Personnel\Vendor::class,
'region' => \App\Models\Location\Region::class,
'district' => \App\Models\Location\District::class,
'agency' => \App\Models\DailyLogs\Agency::class,
'incident' => \App\Models\Incidents\Incident::class,
'user' => \App\Models\User::class,
]);
Relation::enforceMorphMap([
'vendor' => \App\Models\Personnel\Vendor::class,
'region' => \App\Models\Location\Region::class,
'district' => \App\Models\Location\District::class,
'agency' => \App\Models\DailyLogs\Agency::class,
'incident' => \App\Models\Incidents\Incident::class,
'user' => \App\Models\User::class,
]);
If you don't have a morph map ... then I dunno.
Abi
AbiOP16mo ago
ok that worked
cheesegrits
cheesegrits16mo ago
I think the last 3 in that map above I had to add just to shut LW3 up.
Abi
AbiOP16mo ago
Thank you. do you use Spatie's permission package?
Abi
AbiOP16mo ago
Ralph J. Smit
How to fix the Laravel “No morph map defined for model” error | RJS
In this tutorial I'll show you how to fix the Laravel "No morph map defined for model"-error quick & easy. I'll also explain why the error in Laravel exists.
Abi
AbiOP16mo ago
changing to morphMap instead of enforceMorphMap resolves this issue without having those.
cheesegrits
cheesegrits16mo ago
Ah, interesting! Thank you! And thank @ralphjsmit. Yes, I do use Spatie permissions.
Abi
AbiOP16mo ago
thats when it started creating this issue. I am assuming they use morphs
Dennis Koch
Dennis Koch16mo ago
They do
cheesegrits
cheesegrits16mo ago
That's definitely a really useful tip from Ralph. Now I can just switch to using morphMap() instead of enforceMorphMap() and not worry about it.
Want results from more Discord servers?
Add your server