Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hotfuzz123 opened this issue Jun 16, 2021 · 5 comments
Closed

Comments

@hotfuzz123
Copy link

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
Copy link

@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
Copy link
Author

@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
Copy link

@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 $slider object 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
Copy link
Author

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

  • add public_id as a field in database column.
  • add Cloudinary::destroy();
  • Here is my example:
    Screenshot 2021-06-18 020705

@dela-dels
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants