Delete the row after removing file from storage

everything works perfectly but it throw an error “Trying to get property ‘path’ of non-object”
blade on this line “unlink(“uploads/”.$img->path);” and when I refresh the row and file are deleted

<table class="table table-hover">



      <th scope="col">image</th>


      <th scope="col">title</th>
      <th scope="col">Actions </th>






  >@foreach($data as $img)


    <th scope="row"> <img src="uploads/{{ $img->path }}" width="50%" /></th>


      <a href='#'><i class="fa fa-edit"    id="updateIcon"   wire:click="selectItem({{ $img->id }}, 'update')" ></i></a>


      <a href="#"><i class="fa fa-trash" style='color:red;'    wire:click="selectItem({{ $img->id }}, 'delete')"  data-target="#modalFormDelete"></i></a>




    <div class="modal fade" id="modalFormDelete" tabindex="-1" aria-labelledby="modalFormDeletePost" aria-hidden="true">

        <div class="modal-dialog">

            <div class="modal-content">

            <div class="modal-header">

                <h5 class="modal-title" id="modalFormDeletePost">Delete Item</h5>

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                <span aria-hidden="true">&times;</span>



            <div class="modal-body">

                <h3>Are you sur ?</h3>


            <div class="modal-footer">

                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>

                <button wire:click="delete" type="button" class="btn btn-danger"><i class="fa fa-trash" style='color:white;' aria-hidden="true" ></i> Yes</button>








class :

    public function selectItem($itemId, $action)


            $this->selectedItem = $itemId;

            if ($action == 'delete') {






        public function delete()







Hey, @hamza
What is the type of selectedItem?

Try to do the following

$image = CarouseelImg::where('id', $yourId)->first();

hey @skywalker,
the selectedItem is id of the model Caroussel_Img
i tried your solution but it throw the same error “Trying to get property ‘path’ of non-object” but when i refresh the row and the file are deleted successfully

Try to dd the image variable a see the result


try to see all the records inside the Caroussel_Img and compare your id with the id’s from the returned collection


dd($img, $this->selectedItem);

In another word, try to debug your code line by line and figure out the problem and if you find anything you didn’t understand feel free to ask me and I’ll help you as soon as I can.

hey @skywalker thanks for replaying again,
i tried you suggestion and i debugged my code line by line the id match $this->selectedItem but it throw a error and when i refresh, the row from database and the image from the public folder both are deleted !!

i found a solution is not a good one but it did the work for me
just add @ beside the unlink() method

public function delete()

Hiding the error is not a good solution

You delete the image, but $data still includes it

@foreach($data as $img)

You don’t show the code where $data is initialised, but you need to reinitialise it now that a member has been deleted.

1 Like