Laravel 12 E-Commerce Project Tutorial
Admin Create Coupons Page
Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. In this video, we will be learning how to create the Admin Coupons Page. Let'swalk through the steps to create this page.
-
Create Coupon Model and Migration
First, we need to create the
CouponModel and its corresponding migration.Run the following command in your command prompt:
public function coupons()
{
$coupons = Coupon::orderBy("expiry_date","DESC")->paginate(12);
return view("admin.coupons",compact("coupons"));
}This command will successfully create both the model and the migration file.
-
Create the AdminController Function
Next, let'screate a new function within the
AdminControllerto handle the display of all coupons:Route::get("/admin/coupons",[AdminController::class,"coupons"])->name("admin.coupons"); -
Define the Route
Now, define the route for accessing the admin coupons page:
<x-admin-layout>
</x-admin-layout> -
Create the View File
Go to the
resources/views/admindirectory and create a new view file namedcoupons.blade.php.In this file, extend the admin layout:
<tbody>
@foreach ($coupons as $coupon)
<tr>
<td scope="row">{{$coupon->id}}</td>
<td>{{$coupon->code}}</td>
<td>{{$coupon->type}}</td>
<td>{{$coupon->value}}</td>
<td>{{$coupon->cart_value}}</td>
<td>{{$coupon->expiry_date}}</td>
<td>
<div class="list-icon-function" bis_skin_checked="1">
<a href="{{route("admin.coupon.edit",["id"=>$coupon->id])}}">
<div class="item edit" bis_skin_checked="1">
<i class="fa fa-edit"></i>
</div>
</a>
<form action="{{route("admin.coupon.delete",["id"=>$coupon->id])}}" method="POST">
@csrf
@method("DELETE")
<button type="submit" style="background:none; border:none; padding:0;">
<div class="item text-danger delete" bis_skin_checked="1">
<i class="fa fa-trash"></i>
</div>
</button>
</form>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
{{-- Note: The original code had $categories->links(), which should be $coupons->links() --}}
{{$coupons->links("pagination::bootstrap-5")}}
</div>Then, copy the main content structure (the
divwith the classmain-content) from yourcoupons.htmltemplate file and paste it inside thecoupons.blade.phplayout section.Ensure you add the dashboard link using
{{route("admin.index’)}}where appropriate in the view. -
Displaying Coupons Data in the View
Inside the view, implement the loop to display the coupon data retrieved from the controller. The table body should look like this:
pacehoder_5 -
Update Admin Layout
Finally, open your admin layout file and add the link for the coupons page using
{{ route("admin.coupons") }}in the navigation menu.
Verification
After completing these steps, go to your admin page and click on the Coupons link. You should now see the coupons page. Since no coupons are currently stored in the table, the list will be empty. In the next video, we will cover how to add a new coupon.
This concludes the process for creating the Admin Coupons Page.
