Laravel 12 E-Commerce: Implementing Admin Create New Category

Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. This video covers the steps for implementing the functionality to add a new category to the admin panel.


1. Create Controller Function and View

Create View Function (`AdminController.php`)

In the `AdminController`, create a function to render the "add category" view:

public function add_category()
{
return view("admin.category-add");
}

Define Route for View (`web.php`)

Create the GET route for accessing the new category form:

Route::get("/admin/category/add",[AdminController::class,"add_category"])->name("admin.category.add’);

Create and Setup View (`category-add.blade.php`)

Create the view file at `resources/views/admin/category-add.blade.php`. For simplicity, copy all content from `brand-add.blade.php` and paste it here, as the form structure is identical.


2. Implement Category Store Logic (`AdminController.php`)

Store Function (`add_category_store`)

In `AdminController.php`, create a POST function to handle form submission. This function validates the input, generates a slug, processes the image using a helper function (like `GenerateCategoryThumbailImage`), and saves the new category to the database.

public function add_category_store(Request $request)
{
$request->validate([
"name" => "required",
"slug" => "required|unique:categories,slug",
"image" => "mimes:png,jpg,jpeg|max:2048"
]);
$category = new Category();
$category->name = $request->name;
$category->slug = Str::slug($request->name);
$image = $request->file("image");
$file_extention = $request->file("image")->extension();
$file_name = Carbon::now()->timestamp . "." . $file_extention;
$this->GenerateCategoryThumbailImage($image, $file_name);
$category->image = $file_name;
$category->save();
return redirect()->route("admin.categories")->with("status", "Category has been added successfully!");
}

Define Route for Store (`web.php`)

Define the POST route that the form will submit to:

Route::post("/admin/category/store",[AdminController::class,"add_category_store"])->name("admin.category.store’);

3. Update View Form and Links

Update Form Action (`category-add.blade.php`)

Update the form action in `category-add.blade.php` to point to the new store route:

<form action="{{route("admin.category.store")}}" method="POST" enctype="multipart/form-data" data-parsley-validate>

Add "Add New" Link to Categories Page (`categories.blade.php`)

Open the `categories.blade.php` view file and add a link/button to navigate to the new category creation page:

<div class="col-6">
<a href="{{route("admin.category.add")}}" class="btn btn-sm btn-outline-primary float-end" style="display: flex; align-items: center; vertical-align: middle;">
<i class="material-icons" style="margin-right: 4px;">add</i>
Add New
</a>
</div>

JavaScript for Slug Generation (Ensure it'sincluded)

Ensure the JavaScript for automatic slug generation (copied from the brand view) is present and functional in `category-add.blade.php`.


4. Testing

After implementing all changes, navigate to the Admin Categories page, click on Add New, fill in the category name and select an image, and click Submit. The new category should appear in the list with a success message.

This completes the process for creating new categories in the admin panel.