ravipw1801's avatar

Deleting/Updating a file already present on the database

Building a laravel app, User can create multiple post & can upload only 1 image. In case he wishes to update later, he can do it easily, but if in case he completely wants to remove it, how this will take place?

http://prntscr.com/l18h2w

As you can see in the screenshot, It has a remove icon, but how to ACTUALLY remove the image? and replace it with default image like 'no-image.jpg'?

0 likes
4 replies
Tomi's avatar

You make an ajax, or post request to your server hitting a specific url lie:

`user/{user}/image/delete``

this will then call the controller function deleteUserImage

public function deleteUserImage(User $user) {

   $user->image = '' ; // or set it to default image what ever
   $user->save();

    // then here you probably remove the file from your server  

   return response('success');
}
ravipw1801's avatar

Hey @Tomi I have already tried doing so, but am not getting expected results.

This is what I have tried so far:

Ajax:

<script>
    function delete_image()
    {
        var status = confirm("Are you sure you want to delete ?");  
        if(status==true)
        {
            $.ajax({
                headers: {
                'X-CSRF-TOKEN': $("input[name='_token']").val()
                },
                type:"POST",
                url:"{{route('business.dellogo',$business->id)}}",
                success: function(data){    

                }
            });
        }
    }
</script>

BusinessController:

public function destroyPhoto($id)
    {
        $business = Business::find($id);     
        $business->logoimg == 'no-logo.jpg';

     return true;
     
    }

Am not recieving any error, but it is also not saving as default image 'no-logo.jpg'

Snapey's avatar
Snapey
Best Answer
Level 122

this compares the logo image

        $business->logoimg == 'no-logo.jpg';

Then, you don't save $business

1 like

Please or to participate in this conversation.