Hi,
My Post can have multiple photos. I want to delete one media
i try
$media = Media::find($media_id)->delete();
and
$post->deleteMedia($media_id);
Is there a way to delete each specific image of Post without deleting all images in Post Model.
for example, I have 4 images and each image has a remove/delete button and when i click on the delete button it should remove just that image and its related conversion both from db and from the directory. Your solution alongside others would remove the image from db however, it would also delete all images in the folder.
so my question is - is there a way to remove each image using medialibrary?
e.g 5 images, user remove 1, remaining images should be 4 inside the folder, but currently, the folder that contains the images to that model wud be deleted. Any hint for this?
Am using v7.6 laravel 5.8.13
This is not an issue. This is depends on what type of workflow u choose in ur app.
For a simple example , in form where user can choose wich img to delete u can add checkbox to each image
<label>
<input class="mediaTodelete" type="checkbox" name='mediaTodelete[]' value="{{$img->id}}" > to delete
</label>
And on backend smthng like this:
$toDeleteIds = $request->mediaTodelete;
if(count($toDeleteIds)) {
$mediaTodelete = Media::whereIn('id', $toDeleteIds)->delete();
}
I expect @neuotq is sufficient?
Hi
i have same issue
$media = Media::whereName($name)->first();
$media->delete();
yes it deleting one record in database however deleting also all files
Same problem here. Delete record in database but not the files.
It's not clear whether you have the same issue or the opposite one. Please open a PR with a failing test or provide a full example of what you expect the behaviour to be.
@brendt @demeus i have the same issue.
i am using custom path generator and here is my folder structure,
media/profile_photo/1/file_name.jpg
where 1 is user_id means $media->model_id , i have total 5 images into media/profile_photo/1/...
while deleting one of them 1/.. folder removed with all 5 images, but in DB only specific records is removed.
One media item is always stored in a separate folder by default. Please refer to https://docs.spatie.be/laravel-medialibrary/v7/advanced-usage/using-a-custom-directory-structure/#using-a-custom-directory-structure
Most helpful comment
This is not an issue. This is depends on what type of workflow u choose in ur app.
For a simple example , in form where user can choose wich img to delete u can add checkbox to each image
And on backend smthng like this: