I'm intending to delete a Category in my database, I have succeeded in doing that by using Livewire Components but I'm trying to make it by using Controllers. This is my Controller.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;
use App\Models\Staff;
use App\Models\User;
use App\Models\Category;
use Livewire\WithPagination;
use Haruncpi\LaravelIdGenerator\IdGenerator;
use DB;
class CategoriesController extends Controller
{
use WithPagination;
public function deleteCategory($id)
{
$category = Category::find($id);
$category->delete();
session()->flash('message', 'Category has been deleted successfully !');
}
public function index()
{
$categories = Category::paginate(10);
return view('form.categories', ['categories' => $categories]);
}
}
And here I have a blade(not Livewire's):
@foreach ($categories as $category)
<tr>
<td>{{$category->id}}</td>
<td>{{$category->name}}</td>
<td>{{$category->slug}}</td>
<td>
<a href="{{route('admin.editcategory', ['category_slug'=> $category->slug])}}"><i class="fa fa-edit "></i></a>
<a href="#" onclick="confirm('Are you sure ,You want to delete this Category ?') || event.stopImmediatePropagation()" wire:click.prevent="deleteCategory({{$category->id}})"><i class="fa fa-times text-danger"></i></a>
</td>
</tr>
@endforeach