0

I am new in laravel livewire and i think my controller isn’t able to call (id) in my function

but what drives me crazy is the last part function render() in the same controller works fine

livewire controller:

<?php

namespace App\Http\Livewire\Admin\Category;

use App\Models\Category;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Facades\File;


class Index extends Component
{
    use WithPagination;

    protected $paginationTheme =  'bootstrap';

    public $category_id;

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

    public function destroyCategory()
    {
        $category = Category::find($this->category_id);
        $path = 'uploads/category/' . $category->image;

        if (File::exists($path)) {
            File::delete($path);
        }

        $category->delete();

        session()->flash('message', 'Category Deleted');
    }

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

        return view('livewire.admin.category.index', ['categories' => $categories]);
    }
}

livewire view:

<div>
    <div wire:ignore.self 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" 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-secondary" >Delete</button>
                    </div>
                </form>
            </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">
                <div class="card-header">
                    <h2>Category <a href="{{url('admin/category/create')}}" class="btn btn-primary btn-sm float-end">Add Category</a></h2>
                </div>
                <div class="card-body">
                    <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}})" data-bs-toggle="modal" data-bs-target="#deleteModal" class="btn btn-danger">Delete</a>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                    <div>
                        {{ $categories->links('pagination::bootstrap-4') }}
                    </div>
                </div>
            </div>
        </div> 
    </div>
</div>    

I’m trying to make a delete button in my livewire.blade and call its function(destroyCategory) from livewire controller without using routs every time i try to delete it doesn’t give me any thing but a question mark with my link like this http://127.0.0.1:8000/admin/category?

IGP
  • 14,160
  • 4
  • 26
  • 43
JoyBoy
  • 31
  • 3

1 Answers1

0

As pointed out in a comment, your syntax for wire:click is wrong.

<a href="#" wire:click="deleteCategory({{$category->id}})" ...>Delete</a>

Also, you're resolving to a model and then acting as if you only have the id in deleteCategory.

Either do

public function deleteCategory(Category $category)
{
    $this->category_id = $category->id;
}

or

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

Why is this important? if $this->category_id is a Category model, then when you do $category = Category::find($this->category_id); in destroyCategory, $category will end up being a Collection, which means the line

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

will no longer work since image is not a property the collection has.

IGP
  • 14,160
  • 4
  • 26
  • 43