Search code examples
laravelimagefile-uploadcloudinarylaravel-nova

Laravel Nova upload image and save url to database


I have been attempting to upload an image to cloudinary, which is pretty easy. My problem is how do I go about saving the url to the database instead of the image? I was suppose to use https://github.com/Silvanite/nova-field-cloudinary, but the documentation is pretty slim. Also, I would like to save the image with original file name (storeOriginalName).

CloudinaryImage::make('Featured Image')

Nova's version:

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. This works for storing original file name, but still not sure how to grab url and save it to featured_image column:

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

Solution

  • You shouldn't need to store the remote URL with Cloudinary. The public id returned by the component is used to generate the final URL when you output the image somewhere using one of the ways described in the documentation ...

    // 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",
        ],
    ])
    

    Or you could generate the URL yourself as per the Cloudinary documentation https://cloudinary.com/documentation/image_optimization

    It would be helpful if you could expand on why you need to save the full URL as there may be an alternative solution.