Laravel Nova 每次编辑都需要上传图片

ST8*_*T80 4 php laravel laravel-nova

我正在使用laravel-novaImage领域的一项资源:

use Laravel\Nova\Fields\Image;

Image::make('Top Image', 'hero_image')
      ->help('Upload an image to display as hero')
      ->disk('local')
      ->maxWidth(400)
      ->prunable()
      ->rules('required')
      ->hideFromIndex(),
Run Code Online (Sandbox Code Playgroud)

到目前为止一切顺利,但由于它是必需的,所以每次我想编辑资源时都必须上传(相同的)图像,这有点烦人,我不想让它不需要。

那么,有没有办法解决这个问题?

Chi*_*ung 6

首先,您希望仅在创建时才需要它,因此您应该使用->creationRules('required')而不是->rules('required').

但是接下来的问题是用户可以删除照片,并保存没有图像的资源。

要解决这个问题,您只需使用->deletable(false).

Image::make('Top Image', 'hero_image')
    ->help('Upload an image to display as hero')
    ->disk('local')
    ->maxWidth(400)
    ->prunable()
    ->creationRules('required')
    ->deletable(false)
    ->hideFromIndex(),
Run Code Online (Sandbox Code Playgroud)

这将允许您更新您的资源,而不必每次都上传图像。并且用户只能用另一个图像替换原始图像。