Welcome back to the Laravel E-Commerce Project Tutorial. In this video, we will learn about creating a new coupon.

Step 1: Create a Function in the AdminController

First, let's create a new function in the AdminController:


public function add_coupon()
{
return view(`admin.coupon-add`);
}

Step 2: Create a Route for the Function

Next, create a route for this function:


Route::get('/admin/coupon/add',[AdminController::class,'add_coupon'])->name('admin.coupon.add');

Step 3: Create a View for Adding a New Coupon

Now, go to the resources directory and create a new view file called coupon-add.blade.php. In this file, extend the layout:


@extends('layouts.admin')
@section('content')
@endsection

Step 4: Add Content to the Coupon Add View

Now, go to the template directory and open the add-coupon.html file in a text editor. Copy the div with the class `main-content` and paste it inside the coupon-add.blade.php file, making the following changes:


@extends('layouts.admin')
@section('content')
<div class=`main-content-inner`>
<!-- main-content-wrap -->
<div class=`main-content-wrap`>
<div class=`flex items-center flex-wrap justify-between gap20 mb-27`>
<h3>Coupon infomation</h3>
<ul class=`breadcrumbs flex items-center flex-wrap justify-start gap10`>
<li>
<a href=`{{route('admin.index')}}`><div class=`text-tiny`>Dashboard</div></a>
</li>
<li>
<i class=`icon-chevron-right`></i>
</li>
<li>
<a href=`{{route('admin.coupons')}}`><div class=`text-tiny`>Coupons</div></a>
</li>
<li>
<i class=`icon-chevron-right`></i>
</li>
<li>
<div class=`text-tiny`>New Coupon</div>
</li>
</ul>
</div>
<!-- new-category -->
<div class=`wg-box`>
<form class=`form-new-product form-style-1` method=`POST` action=`{{route('admin.coupon.store')}}` >
@csrf
<fieldset class=`name`>
<div class=`body-title`>Coupon Code <span class=`tf-color-1`>*</span></div>
<input class=`flex-grow` type=`text` placeholder=`Coupon Code` name=`code` tabindex=`0` value=`{{old('code')}}` aria-required=`true`>
</fieldset>
@error(`code`) <span class=`alert alert-danger text-center`>{{$message}}</span> @enderror
<fieldset class=`category`>
<div class=`body-title`>Coupon Type</div>
<div class=`select flex-grow`>
<select class=`` name=`type`>
<option value=``>Select</option>
<option value=`fixed` @if(old('type')==`fixed`) {{`selected`}} @endif>Fixed</option>
<option value=`percent` @if(old('type')==`percent`) {{`selected`}} @endif>Percent</option>
</select>
</div>
</fieldset>
@error(`type`) <span class=`alert alert-danger text-center`>{{$message}}</span> @enderror
<fieldset class=`name`>
<div class=`body-title`>Value <span class=`tf-color-1`>*</span></div>
<input class=`flex-grow` type=`text` placeholder=`Coupon Value` name=`value` tabindex=`0` value=`{{old('value')}}` aria-required=`true`>
</fieldset>
@error(`value`) <span class=`alert alert-danger text-center`>{{$message}}</span> @enderror
<fieldset class=`name`>
<div class=`body-title`>Cart Value <span class=`tf-color-1`>*</span></div>
<input class=`flex-grow` type=`text` placeholder=`Cart Value` name=`cart_value` tabindex=`0` value=`{{old('cart_value')}}` aria-required=`true`>
</fieldset>
@error(`cart_value`) <span class=`alert alert-danger text-center`>{{$message}}</span> @enderror
<fieldset class=`name`>
<div class=`body-title`>Expiry Date <span class=`tf-color-1`>*</span></div>
<input class=`flex-grow` type=`date` placeholder=`Expiry Date` name=`expiry_date` tabindex=`0` value=`{{old('expiry_date')}}` aria-required=`true`>
</fieldset>
@error(`expiry_date`) <span class=`alert alert-danger text-center`>{{$message}}</span> @enderror

<div class=`bot`>
<div></div>
<button class=`tf-button w208` type=`submit`>Save</button>
</div>
</form>
</div>
<!-- /new-category -->
</div>
<!-- /main-content-wrap -->
</div>
</div>
@endsection

Step 5: Create a Function for Storing the Coupon

Now, go to the AdminController and create a new function for storing the 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`)->with('status','Record has been added successfully !');
}

Step 6: Create a Route for Storing the Coupon

Next, create a route for this function:


Route::post('/admin/coupon/store',[AdminController::class,'add_coupon_store'])->name('admin.coupon.store');

Step 7: Add Form and Validation to the Coupon Add View

Now, go to the coupon-add.blade.php file and add the following code inside the form:

method=`POST` action=`{{route('admin.coupon.store')}}`

Display validation message:

@error(`code`) {{$message}} @enderror

After submitting the form, display the entered value:

value=`{{old('value')}}`

Step 8: Add a Link to the Coupons View

Now, go to the coupons.blade.php file and add a link to add a new coupon:


<a class=`tf-button style-1 w208` href=`{{route('admin.coupon.add')}}`><i class=`icon-plus`></i>Add new</a>

Now it's done! Let's check it out. Refresh the page and go to the URL /admin/coupon/add. Enter the coupon details, such as code, type, value, cart value, and expiry date. Click on save, and the coupon will be added.

In this way, you can create a new coupon.

That's all about creating a new coupon.