How to delete a category in Laravel

Viewed 216

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

If you want to do that in a controller, you should have a link, instead of treating it as it was still a LiweWire component and calling the method directly. The route() call, will generate the URL you need and let your tag link to that route.

<a href="{{route('category.delete', $category->id)}}" onclick="confirm('Are you sure ,You want to delete this Category ?') || event.stopImmediatePropagation()">
     <i class="fa fa-times  text-danger"></i>
</a>

For this to work you will need a route to actually trigger your delete method. In web.php routes file add that route.

Route::get('category/delete/{id}', [CategoriesController ::class, 'deleteCategory']);

For user experience, i would redirect back after it is deleted.

public function deleteCategory($id)
{
    $category = Category::find($id);
    $category->delete();
    session()->flash('message', 'Category has been deleted successfully !');

    return redirect()->back();
}

BE AWARE

I would not consider this approach the best solution, usually you would do delete operations with the DELETE method on the Http call. As this seem to be your first step into Controllers, i think this will do fine, as doing DELETE methods in Http require forms.

Create a route in your web.php file Route::get(“deleteCategory/{id}”,[CategoriesController::class,” deleteCategory”)->name(“ deleteCategory”);

make the href of the a tag href=“{route(‘ deleteCategory’,$category->id)}”

ps 1-For the delete route it’s better to use form method delete instead of a get request

2-check also how to make a resource controller and resource routes in laravel

Related