Laravel 12 E-Commerce: Creating the Admin Brand Page
Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. In this video, we will learn about creating the Brand management page for the admin panel.
1. Create Model and Migration for Brand
First, let'screate a new Brand model and its associated database migration.
Create Model
Run the following command in the terminal:
php artisan make:model Brand –m Update Brand Migration
Open the generated migration file (inside the database/migrations directory) and add the necessary columns to the brands table within the up method:
public function up(): void
{
Schema::create("brands", function (Blueprint $table) {
$table->id();
$table->string("name");
$table->string("slug")->unique();
$table->string("parent_id")->nullable();
$table->string("image")->nullable();
$table->timestamps();
});
} Run Migration
Execute the migration to create the brands table in the database:
php artisan migrate 2. Controller Function and Route Setup
Create Controller Function
In the AdminController.php file, create a brands() function to fetch all brands and paginate the results:
public function brands()
{
$brands = Brand::orderBy("id", "DESC")->paginate(10);
return view("admin.brands", compact("brands"));
} Define Route
Open routes/web.php and define the route for the admin brands page, ensuring it uses the correct middleware (as set up in previous parts):
Route::get("/admin/brands", [AdminController::class, "brands"])->name("admin.brands"); 3. Create View and Integrate Template
Create Brands View
Create a new view file: resources/views/admin/brands.blade.php. Extend the admin layout:
<x-admin-layout>
</x-admin-layout> Integrate HTML Template Content
Open the HTML template file (admin/brands.html). Find and copy the content inside the main-content div and paste it inside the x-admin-layout component in brands.blade.php.
Update Dashboard Link
Update the link to the Admin Dashboard within the view:
{{ route(‘admin.index’) }} Display Brands in Table
Replace the static table content with a loop to display the dynamically fetched brands:
<tbody>
@foreach ($brands as $brand)
<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/brands") . "/" . $brand->image }}" alt="{{$brand->name}}"/>
</div>
<span class="f_s_12 f_w_600 color_text_5">{{$brand->name}}</span>
</div>
</td>
<td>{{$brand->slug}}</td>
<td>0</td>
<td>
<div class="list-icon-function">
<a href="{{route("admin.brand.edit", ["id" => $brand->id])}}">
<div class="class item_edit">
<i class="fa fa-edit"></i>
</div>
</a>
<form action="#" method="POST">
@csrf
<div class="class item_text-danger delete">
<i class="fa fa-trash"></i>
</div>
</form>
</div>
</td>
</tr>
@endforeach
</tbody> Add Pagination
Insert the pagination links at the bottom of the table, specifying the Bootstrap 5 style since the template uses it:
<div class="flex items-center justify-between flex-wrap gap10 wgp-pagination">
{{ $brands->links("pagination::bootstrap-5") }}
</div> 5. Final Check
After saving all files and navigating to the Admin Brands page in the browser, you should see the brands page correctly displayed with the admin template applied. This completes the setup for the admin brands page.
