Laravel 12 E-Commerce Project Tutorial
Admin: Creating a New Coupon
Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. In this video, we"ll learn how to create a new coupon and store it in the database.
-
Create the View Function in AdminController
First, open your
AdminControllerand create a new function,add_coupon, to load the form view:public function add_coupon()
{
return view("admin.coupon-add");
} -
Define the Route for the View
Next, define the route to access the "Add New Coupon" page:
Route::get("/admin/coupon/add",[AdminController::class,"add_coupon"])->name("admin.coupon.add"); -
Create the Coupon Add View (
coupon-add.blade.php)Create a new file named
coupon-add.blade.phpin theresources/views/admindirectory and extend the admin layout:<x-admin-layout>
</x-admin-layout>Copy the content (the
divwith classmain-content) from your template'sadd-coupon.htmlfile into this section, and ensure the dashboard link is correctly set using{{route("admin.index’)}}. -
The Coupon Creation Form
Implement the form structure within the view. This form uses the POST method and submits data to the store route. We include fields for the coupon details, validation messages, and use `data-parsley-validate` for client-side validation.
<div class="white_card_body">
<form method="POST" action="{{route("admin.coupon.store")}}" data-parsley-validate>
@csrf
<div class="row mb-3">
<div class="col-md-6">
<label class="form-label" for="code">Coupon Code</label>
<input type="text" class="form-control" name="code" value="{{old("code")}}" placeholder="" required>
@error("code") <span class="alert alert-danger text-center">{{$message}}</span> @enderror
</div>
<div class="col-md-6">
<label class="form-label" for="type">Coupon Type</label>
<select class="form-select" name="type" required>
<option value="">Select Coupon Type</option>
<option value="fixed" @if(old("type") == "fixed") selected @endif>Fixed Amount</option>
<option value="percent" @if(old("type") == "percent") selected @endif>Percentage</option>
</select>
@error("type") <span class="alert alert-danger text-center">{{$message}}</span> @enderror
</div>
</div>
<div class="row mb-3">
<div class="col-md-6">
<label class="form-label" for="value">Value</label>
<input type="text" class="form-control" name="value" value="{{old("value")}}" placeholder="" required>
@error("value") <span class="alert alert-danger text-center">{{$message}}</span> @enderror
</div>
<div class="col-md-6">
<label class="form-label" for="cart_value">Cart Value (Minimum)</label>
<input type="text" class="form-control" name="cart_value" value="{{old("cart_value")}}"
placeholder="" required>
@error("cart_value") <span class="alert alert-danger text-center">{{$message}}</span> @enderror
</div>
</div>
<div class="row mb-3">
<div class="col-md-6">
<label class="form-label" for="expiry_date">Expiry Date</label>
<input type="date" class="form-control" name="expiry_date" value="{{old("expiry_date")}}" required>
@error("expiry_date") <span class="alert alert-danger text-center">{{$message}}</span> @enderror
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div> -
Create the Coupon Store Function
Now, create the
add_coupon_storefunction in theAdminControllerto handle the form submission and store the new coupon.public function add_coupon_store(Request $request)
{
$request->validate([
"code" => "required",
"type" => "required",
"value" => "required|numeric",
"cart_value" => "required|numeric",
"expiry_date" => "required|date"
]);
$coupon = new Coupon();
$coupon->code = $request->code;
$coupon->type = $request->type;
$coupon->value = $request->value;
$coupon->cart_value = $request->cart_value;
$coupon->expiry_date = $request->expiry_date;
$coupon->save();
return redirect()->route("admin.coupons");
} -
Define the Store Route
Define the POST route that points to the new store function:
Route::post("/admin/coupon/store",[AdminController::class,"add_coupon_store"])->name("admin.coupon.store");
Verification and Testing ✨
After implementing the code, refresh the page and navigate to /admin/coupon/add. Enter the required coupon details, such as:
- Coupon Code: `OFF10`
- Coupon Type: `Fixed` or `Percent`
- Value: `10`
- Cart Value (Minimum): `100`
- Expiry Date: Set a future date
Click on Submit. The form will redirect you to the main coupons list, and you should see a message confirming the Coupon has been added.
This completes the process for creating a new admin coupon.
