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">

  <thead>

    <tr>

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

      

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

     

    </tr>

  </thead>

 

  <tbody

  >@foreach($data as $img)

    <tr>

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

      <td>{{$img->title}}</td>
       <td>

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

      &nbsp;&nbsp;

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

      </td>

   @endforeach

    </tr>

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

                </button>

            </div>

            <div class="modal-body">

                <h3>Are you sur ?</h3>

            </div>

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

            </div>

            </div>

        </div>

    </div>

    <div>

           

  </tbody>

class :

    public function selectItem($itemId, $action)

        {

            $this->selectedItem = $itemId;

            if ($action == 'delete') {

                 $this->dispatchBrowserEvent('openDeleteModal');

            }

            

            

        }

        public function delete()

        {

            $img=Caroussel_Img::find($this->selectedItem);

            unlink("uploads/".$img->path);

            Caroussel_Img::where("id",$img->id)->delete();

            $this->dispatchBrowserEvent('closeDeleteModal');

         }

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

$img=Caroussel_Img::find($this->selectedItem);
dd($img->path);

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

$img=Caroussel_Img::all();

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()
{
 $img=Caroussel_Img::find($this->selectedItem);
@unlink("uploads/".$img->path);
Caroussel_Img::destroy($this->selectedItem);
 $this->dispatchBrowserEvent('closeDeleteModal');
}

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