Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nzmattman's avatar

How do i change the image caption for an image field: Nova

Hi all.

So we are using the Image tag and using a custom store.

The image its self isnt' saved against the model, but in a relation table.

I am able to get it saving, previewing and deleting ok, but the only problem left I have is when I view / edit the resource the image field caption is null.

For the life of me I can't work out how to change / fix that.

Here is my code to display the image in the resource

Image::make(__('Image'), 'image')
                ->store(function (NovaRequest $request, $model) {
                    return function () use ($model, $request) {
                        FileAssets::saveFromNova($request->image, 'image', $model, 'resource-centre');
                    };
                })
                ->thumbnail(function () {
                    if ($this->resource->resource_event_id) {
                        if (!$this->resource->relationLoaded('event')) {
                            $this->resource->load('event');
                        }

                        if (isset($this->resource->event->image)) {
                            return $this->resource->event->image;
                        }
                    }

                    $file = $this->resource->files->first();
                    if (!$file) {
                        return '';
                    }

                    return FileAssets::getPrivateLink($file->path, 's3.assets');
                })
                // ->deletable(false)
                ->dependsOn('event', function (
                    Image $field,
                    NovaRequest $request,
                    FormData $formData
                ) {
                    if ($formData->get('event')) {
                        $field->hide()->nullable();
                    }
                })
                ->delete(function (NovaRequest $request, $model) {
                    $files = $model->files;

                    $fileIds = [];
                    // find the files to be removed
                    if ($files->count()) {
                        foreach ($files as $file) {
                            if ($file->pivot->type === 'image') {
                                $fileIds[] = $file->id;
                            }
                        }
                    }

                    FileAssets::deleteByIds($fileIds);

                    return null;
                })
                ->help('Max file size of 3mb')

For the delete, I am just using an observable to force it not to save if an image attribute exists

class ResourceObserver
{
    public function saving(Resource $resource)
    {
        $data = $resource->toArray();
        if (array_key_exists('image', $data)) {
            return false;
        }

        return true;
    }
}
0 likes
0 replies

Please or to participate in this conversation.