Laravel 12 E-Commerce: Creating the Admin Category Page
Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. This video covers the steps for creating the Category management page for the admin panel.
1. Create Model and Migration for Category
First, we create the new Category model and its associated database migration.
Create Model
Run the following command in the terminal:
php artisan make:model Category –m Update Category Migration
Open the generated migration file and add the required columns to the `categories` table within the `up` method:
public function up(): void
{
Schema::create("categories", function (Blueprint $table) {
$table->id();
$table->string("name");
$table->string("slug");
$table->string("parent_id")->nullable();
$table->string("image")->nullable();
$table->timestamps();
});
} Run Migration
Execute the migration to create the `categories` table in the database:
php artisan migrate 2. Controller Function and Route Setup
Create Controller Function (`AdminController.php`)
In the `AdminController`, create a `categories()` function to fetch all categories and paginate the results, similar to the Brand page setup:
public function categories()
{
$categories = Category::orderBy("id","DESC")->paginate(10);
return view("admin.categories",compact("categories"));
} Define Route (`web.php`)
Define the route for accessing the admin categories page:
Route::get("/admin/categories",[AdminController::class,"categories"])->name("admin.categories’); 3. Create View and Integrate Template
Create Categories View
Create a new view file: `resources/views/admin/categories.blade.php`. Extend the admin layout:
<x-admin-layout>
</x-admin-layout> Integrate HTML Template Content
Copy the content from the admin template's`categories.html` file and paste it into `categories.blade.php`.
Display Categories in Table
Replace the static table body with a loop to display the categories, using the `asset()` helper for the image paths:
<tbody>
@foreach ($categories as $category)
<tr>
<th scope="row">{{$loop->iteration}}</th>
<td>
<div class="custom d-flex align-items-center">
<div class="thumb_34 mr-15 mt-0">
<img class="img-fluid radius_50" src="{{asset("uploads/categories")}}/{{$category->image}}" alt="{{$category->name}}"/>
</div>
<span class="f_s_12 f_w_600 color_text_5">{{$category->name}}</span>
</div>
</td>
<td>{{$category->slug}}</td>
<td></td>
<td>
<div class="list-icon-function">
<a href="{{route("admin.category.edit",["id"=>$category->id])}}">
<div class="item edit">
<i class="fa fa-edit"></i>
</div>
</a>
<form action="#" method="POST">
<div class="item text-danger delete">
<i class="fa fa-trash"></i>
</div>
</form>
</div>
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<td colspan="5">
{{$categories->links("pagination::bootstrap-5")}}
</td>
</tr>
</tfoot>
</table>
5. Final Check
After saving all files and navigating to the Admin page, click on Categories. The category page should now display correctly with the admin template applied. In the next video, we will cover how to add new categories.
This completes the setup for the Admin Category Page.
