Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abcuse's avatar

My Category Delete Does Not Work

I am trying to find a way to delete my category that I have created. I get the error attempt to read image is null, I think the error has to do with category_id being null. How can I retrieve it for my category object?

//code begins
class Index extends Component
{
    use WithPagination;
    protected $paginationTheme = 'bootstrap';

    public $category_id;

    //setting category_id
    public function deleteCategory($category_id)
    {
        $this->category_id = $category_id;
    }

    
    public function destroyCategory() {
        $category = Category::find($this->category_id);
        // dd($this);
        $path = 'uploads/category/'.$category->image;
        if (File::exists($path)) {
            File::delete($path);
        }
        $category->delete();
        session()->flash('message', 'Category Deleted');
        $this->dispatchBrowserEvent('close-modal');
    }

    public function render()
    {
        $categories = Category::orderBy('id', 'DESC')->paginate(10);

        return view('livewire.admin.category.index', ['categories' => $categories]);
    }
}
//code ends
0 likes
5 replies
jlrdw's avatar

Try with a leading slash

$path = '/uploads/category/'.$category->image;

Also try a dd($path); to make sure it is correct. Compare the dd with actual path and file name. I.e., is the extension there.

Edit:

You many need to look in the network tab.

webrobert's avatar

On the forum Three back ticks to open code. And three to close.

abcuse's avatar

I checked the actual path and the files are there in the public directory in that path. The leading slash does not do anything.

This is the components blade file. I am using livewire as well.

    <div>
        <div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <h1 class="modal-title fs-5" id="exampleModalLabel">Category Delete</h1>
                  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form wire:submit.prevent="destroyCategory">

         
                <div class="modal-body">
                  <h6>Are you sure you want to delete this data?</h6>
                </div>
                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary">Yes. Delete</button>
                </div>
                </form>
              </div>
            </div>
          </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            @if(session('message'))
            <div class="alert alert-success">{{session('message')}}</div>
            @endif
            <div class="card-header">
                <h4>
                    Category
                    <a href="{{url('admin/category/create')}}" class="btn btn-primary btn-sm float end">Add Category</a>
                </h4>
            </div>
            <div class="card-body">
                {{-- display all categories using livewire --}}
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($categories as $category)
                        <tr>
                            <td>{{$category->id}}</td>
                            <td>{{$category->name}}</td>
                            <td>{{$category->status == '1' ? "Hidden" : "Visible"}}</td>
                            <td>
                                <a href="{{url('admin/category/'.$category->id.'/edit')}}" class="btn btn-success">Edit</a>
                                <a href="#" wire:click="deleteCategory({{$category->id}})" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal">
                                    Delete
                                </a>
                        </tr> 
                        @endforeach
                    </tbody>

                </table>
                <div>
                {{$categories->links()}}
                </div>
            </div>
        </div>
      </div>
    </div>
@push('script')
<script>
    window.addEventListener('close-modal', event => {
        $('#deleteModal').modal('hide');
    })
</script>
@endpush

Please or to participate in this conversation.