Laravel Nova 上传图片并保存 url 到数据库

Laravel Nova upload image and save url to database

我一直在尝试将图像上传到 cloudinary,这很容易。我的问题是如何将 url 而不是图像保存到数据库?我本来应该使用 https://github.com/Silvanite/nova-field-cloudinary,但文档非常少。另外,我想用原始文件名(storeOriginalName)保存图像。

CloudinaryImage::make('Featured Image')

Nova 的版本:

Image::make('Featured Image')
        ->disk('cloudinary')

https://nova.laravel.com/docs/2.0/resources/file-fields.html#images https://cloudinary.com/documentation/php_image_and_video_upload#upload_response https://laravel.com/docs/5.7/requests#storing-uploaded-files

Update. 这适用于存储原始文件名,但仍然不确定如何抓取 url 并将其保存到 featured_image 列:

    CloudinaryImage::make('Featured Image')
            ->storeAs(function (Request $request) {
                return $request->featured_image->getClientOriginalName();
            }),

上传响应包含一个 url 字段。这是一个例子:

{
 public_id: 'sample',
 version: '1312461204',
 width: 864,
 height: 564,
 format: 'jpg',
 created_at: '2017-08-10T09:55:32Z',
 resource_type: 'image',
 tags: [], 
 bytes: 9597, 
 type: 'upload', 
 etag: 'd1ac0ee70a9a36b14887aca7f7211737', 
 url: '<url>',
 secure_url: '<secure_url>',
 signature: 'abcdefgc024acceb1c1baa8dca46717137fa5ae0c3',
 original_filename: 'sample'
}

您不需要使用 Cloudinary 存储远程 URL。当您使用文档中描述的方法之一将图像输出到某处时,组件返回的 public id 用于生成最终的 URL ...

// Using the helper (with transformation)

return cloudinary_image($this->featured_image, [
    "width" => 200,
    "height" => 200,
    "crop" => "fill",
    "gravity" => "auto",
])

// Using the Storage Facade (without transformation)

return Storage::disk('cloudinary')->url($this->featured_image);

// Using the Storage Facade (with transformation)

return Storage::disk('cloudinary')->url([
    'public_id' => $this->featured_image,
    'options' => [
        "width" => 200,
        "height" => 200,
        "crop" => "fill",
        "gravity" => "auto",
    ],
])

或者您可以根据 Cloudinary 文档 https://cloudinary.com/documentation/image_optimization

自己生成 URL

如果您能详细说明为什么需要保存完整的 URL,那将会很有帮助,因为可能有替代解决方案。