cloudinary-community / cloudinary-laravel

Laravel SDK for Cloudinary
MIT License
258 stars 70 forks source link

How to get public_id of the images that I had uploaded to cloudinary? #40

Closed hotfuzz123 closed 2 years ago

hotfuzz123 commented 2 years ago

For the getPublicId() - i have to upload it and it will get the public_id but i want to get public_id of the old image to delete it and update a new one. How can i do that - can you give me an example??

dela-dels commented 2 years ago

@hotfuzz123 I think the only way you can do this right now is if you are storing the public_id(which could be the filename) of the uploaded file in your database. Then you can use that to delete the resource from Cloudinary by supplying the public_id to the destroy() method of the Cloudinary API.

So the flow could be something like this (using the example of a user updating their profile picture):

Something along these lines should probably work.

hotfuzz123 commented 2 years ago

@dela-dels I can't find the public_id - The error is: "Missing required parameter - public_id". Can you help me please??

Screenshot 2021-06-17 234627

dela-dels commented 2 years ago

@hotfuzz123 I made mention in my previous comment (see below) that you will need to store the public_id as a field in your database column. you are getting that error because the $sliderobject doesn't have a public_id property. You may have to update your database table to reflect that.

@hotfuzz123 I think the only way you can do this right now is if you are storing the public_id(which could be the filename) of the uploaded file in your database. Then you can use that to delete the resource from Cloudinary by supplying the public_id to the destroy() method of the Cloudinary API.

So the flow could be something like this (using the example of a user updating their profile picture):

  • User enters details in the profile picture form to be updated.
  • Before the actual update is done, you retrieve the public_id or file_name attached to the user in the users table or the media table which comes with the Cloudinary package.
  • Use the public_id to make a destroy call to delete the previous image.
  • upload the new image to Cloudinary and update the respective tables with the new values.

Something along these lines should probably work.

hotfuzz123 commented 2 years ago

I have solved my problem :))) I appreciate your help, thank you so much You should add into your documentation about:

dela-dels commented 2 years ago

Happy to help @hotfuzz123 Just a quick tip. $slider->update($request->all()) should probably be changed to$slider->update($request->validated()) which is much safer, the former, which you are doing is dangerous and can easily let attackers inject unwanted stuff.

That said since you have been able to solve your issue, kindly close this issue. All the best.