问题描述
我一直试图将图像上传到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
更新。此方法用于存储原始文件名,但仍不确定如何获取URL并将其保存到Featured_image列:
CloudinaryImage::make('Featured Image')
->storeAs(function (Request $request) {
return $request->featured_image->getClientOriginalName();
}),
解决方法
您不需要将远程URL与Cloudinary一起存储。当您使用文档中所述的方法之一将图像输出到某处时,该组件返回的公共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,],])
或者您可以根据Cloudinary文档https://cloudinary.com/documentation/image_optimization
自己生成URL。如果您可以解释为什么需要保存完整的URL,这可能会有所帮助,因为可能会有其他解决方案。
,上载响应包含一个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'
}