DEV Community

Cover image for Laravel Cloudinary v2 Release Update
Prosper Otemuyiwa
Prosper Otemuyiwa

Posted on

Laravel Cloudinary v2 Release Update

I recently tagged and released v2 of the Laravel Cloudinary package.

Laravel Cloudinary package install

The Laravel Cloudinary package is an SDK that empowers Laravel developers with the ability to fluently upload, optimize, store, transform and deliver media files with Cloudinary. It also provides an API to easily attach your media files to Laravel Eloquent models.

I'll briefly highlight the changes that shipped with Laravel Cloudinary v2 below:

  1. It fully supports Laravel 9.
  2. Rewrote the Cloudinary Adapter to work with Flysystem v3.
  3. Remove deprecated methods to be compliant with Flystem v3.

A Recap of Laravel Cloudinary features:

1. File Uploads via a frontend Upload Widget

Use the x-cld-upload-button Blade upload button component like so:

<!DOCTYPE html>
<html>
    <head>
        ...
        @cloudinaryJS
    </head>
    <body>
        <x-cld-upload-button>
            Upload Files
        </x-cld-upload-button>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

2. File Uploads via the Controller or a Service Class

Using the Cloudinary Facade

// Upload an Image File to Cloudinary with One line of Code
$uploadedFileUrl = Cloudinary::upload($request->file('file')->getRealPath())->getSecurePath();

// Upload a Video File to Cloudinary with One line of Code
$uploadedFileUrl = Cloudinary::uploadVideo($request->file('file')->getRealPath())->getSecurePath();

// Upload any File to Cloudinary with One line of Code
$uploadedFileUrl = Cloudinary::uploadFile($request->file('file')->getRealPath())->getSecurePath();
Enter fullscreen mode Exit fullscreen mode

Using the cloudinary() helper function

// Upload an image file to cloudinary with one line of code
$uploadedFileUrl = cloudinary()->upload($request->file('file')->getRealPath())->getSecurePath();

// Upload a video file to cloudinary with one line of code
$uploadedFileUrl = cloudinary()->uploadVideo($request->file('file')->getRealPath())->getSecurePath();

// Upload any file  to cloudinary with one line of code
$uploadedFileUrl = cloudinary()->uploadFile($request->file('file')->getRealPath())->getSecurePath();

// Upload an existing remote file to Cloudinary with one line of code
$uploadedFileUrl = cloudinary()->uploadFile($remoteFileUrl)->getSecurePath();
Enter fullscreen mode Exit fullscreen mode

Use the $request object directly like so:

// Store the uploaded file on Cloudinary
$result = $request->file('file')->storeOnCloudinary();

// Store the uploaded file on Cloudinary
$result = $request->file->storeOnCloudinary();

// Store the uploaded file in the "lambogini" directory on Cloudinary
$result = $request->file->storeOnCloudinary('lambogini');

// Store the uploaded file in the "lambogini" directory on Cloudinary with the filename "prosper"
$result = $request->file->storeOnCloudinaryAs('lambogini', 'prosper');
Enter fullscreen mode Exit fullscreen mode

3. Display Uploaded Files

Displaying a file using the public id (otherwise known as stored name).

For instance, if a file was uploaded and stored as themagician, then the public id is themagician.

$url = cloudinary()->getUrl($publicId);

// get url from a file
$url = Cloudinary::getUrl($publicId);

// Blade Image Component for displaying images
<x-cld-image public-id="prosper" width="300" height="300"></x-cld-image> 

// Blade Video Component for displaying videos
<x-cld-video public-id="awesome"></x-cld-video> 
Enter fullscreen mode Exit fullscreen mode

4. Attach Files to Eloquent Models

Please head over to the package's documentation to read more about how it attaches uploaded files to Laravel Eloquent models.

Conclusion

If you're building apps with Laravel, you should explore using this package to handle everything media related in your project.

Feel free to drop your questions, concerns and thoughts in the comment section.

Top comments (1)

Collapse
 
jovialcore profile image
Chidiebere Chukwudi

You dy cook boss