The original developer of this Laravel App left and now I have been handed over the task. The admin is created in Nova which I have zero clue of. We have a User.php resource that can have and image as it's Avatar. The developer is using a pivot table images and has used "MorphOne::make('image')," but it was giving error "HasOne relationship has already filled" yet there are no images relation in DB in images table.
When I change MorphOne to MorphMany, it saves the image but it allows many images to upload too. I'm new to this an rarely have found helpful resource to tackle this. Let me know what I missed to track this issue.
User.php
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
Text::make('Email')
->sortable()
->rules('required', 'email', 'max:254')
->creationRules('unique:users,email')
->updateRules('unique:users,email,{{resourceId}}'),
Password::make('Password')
->onlyOnForms()
->creationRules('required', 'string', 'min:8')
->updateRules('nullable', 'string', 'min:8'),
Boolean::make('Verified By Email'),
DateTime::make('Created At')->format('LLLL')->hideFromIndex(),
BelongsToMany::make('Roles'),
BelongsToMany::make('Permissions'),
MorphOne::make('image'),
MorphOne::make('Location'),
HasMany::make('LogActivity','logs')
];
}
Image.php
public function fields(Request $request)
{
$fields = [
ID::make()->sortable(),
\Laravel\Nova\Fields\Image::make('Src')
->rules('required')
->disk('images')
->prunable(),
MorphTo::make('Image')->types([
Song::class,
Post::class,
User::class,
Event::class,
Competition::class,
])
];
return $fields;
}
I got this error when when the relationship on the Laravel (non-Nova base app) model was set to morphMany, and changing that relationship to morphOne as well fixed it.