Laravel 8 E-Commerce - Admin Add Coupons Expiry Date

In this video, we are going to learn about adding an expiry date to coupons.

So, let's see how we can add an expiry date to coupons.

First, let's add a column inside the coupons table.

For that, let's create a migration for adding a column.

So, switch to the command prompt, and for creating a migration, just run the command:


php artisan make:migration add_expiry_date_to_coupons_table --table=coupons

Now, switch to the project and let's open the migration.

Inside the add_expiry_date_to_coupons_table migration, add the following code:


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class AddExpiryDateToCouponsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('coupons', function (Blueprint $table) {
$table->date('expiry_date')->default(DB::raw('CURRENT_DATE'));
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('coupons', function (Blueprint $table) {
$table->dropColumn('expiry_date');
});
}
}

Now, let's migrate this migration.

In the command prompt, run the command:


php artisan migration

Alright, now run the application:


php artisan serve

Now, switch to the project and let's open the admin-coupons-component.blade.php view file.

Here, add a new column inside the table:


<th>Expiry Date</th>

And inside the table body, add the following td:


<td>{{$coupon->expiry_date}}</td>

Now, save it and let's check this.

So, switch to the browser and refresh the page.

Now, you can see the expire date column.

Now, click on add new coupon and here inside this form, let's add a text box for expiry date input.

So, switch to the project and let's open the admin-add-coupon-component.blade.php file.

And let's create a text field here for expiry date:


<div class="form-group">
<label class="col-md-4 control-label">Expiry Date</label>
<div class="col-md-4" wire:ignore>
<input type="text" id="expiry-date" placeholder="Expiry Date" class="form-control input-md" wire:model="expiry_date" />
@error('expiry_date') <p class="text-danger">{{$message}}</p> @enderror
</div>
</div>

After this, in this view file, on the page bottom, add the following code:


@push('scripts')
<script>
$(function(){
$('#expiry-date').datetimepicker({
format: 'Y-MM-DD'
})
.on('dp.change',function(ev){
var data = $('#expiry-date').val();
@this.set('expiry_date',data);
});
});
</script>
@endpush

Now, go to the AdminAddCouponComponent.php Class File.

And let's create a property:


public $expiry_date

Now, inside the updated and storeCoupon function, make the following changes:


public function updated($fields)
{
$this->validateOnly($fields,[
'code' => 'required|unique:coupons',
'type' => 'required',
'value' => 'required|numeric',
'cart_value' => 'required|numeric',
'expiry_date' => 'required'
]);
}

public function storeCoupon()
{
$this->validate([
'code' => 'required|unique:coupons',
'type' => 'required',
'value' => 'required|numeric',
'cart_value' => 'required|numeric',
'expiry_date' => 'required'
]);
$coupon = new Coupon();
$coupon->code = $this->code;
$coupon->type = $this->type;
$coupon->value = $this->value;
$coupon->cart_value = $this->cart_value;
$coupon->expiry_date = $this->expiry_date;
$coupon->save();
session()->flash('message','Coupon has been created successfully!');
}

Now, save it and let's check it.

So, switch to the browser and refresh the page.

Now, let's add a new coupon.

And enter here the coupon code, now select the expiry date, now click on submit.

You can see that the coupon has been created.

Now, let's add this expiry date on the edit coupon component.

So, for that, go to the admin-edit-coupon-component.blade.php view file.

And here, add the following code:


<div class="form-group">
<label class="col-md-4 control-label">Expiry Date</label>
<div class="col-md-4" wire:ignore>
<input type="text" id="expiry-date" placeholder="Expiry Date" class="form-control input-md" wire:model="expiry_date" />
@error('expiry_date') <p class="text-danger">{{$message}}</p> @enderror
</div>
</div>

And add the following code on the page bottom:


@push('scripts')
<script>
$(function(){
$('#expiry-date').datetimepicker({
format: 'Y-MM-DD'
})
.on('dp.change',function(ev){
var data = $('#expiry-date').val();
@this.set('expiry_date',data);
});
});
</script>
@endpush

Now, let's open the AdminEditCouponComponent.php class file.

And here, let's create the property.

Now, inside the mount, updated, and updateCoupon method, make the following changes:


public function mount($coupon_id)
{
$coupon = Coupon::find($coupon_id);
$this->code = $coupon->code;
$this->type = $coupon->type;
$this->value = $coupon->value;
$this->cart_value = $coupon->cart_value;
$this->coupon_id = $coupon->id;
$this->expiry_date = $coupon->expiry_date;
}

public function updated($fields)
{
$this->validateOnly($fields,[
'code' => 'required',
'type' => 'required',
'value' => 'required|numeric',
'cart_value' => 'required|numeric',
'expiry_date' => 'required'
]);
}

public function updateCoupon()
{
$this->validate([
'code' => 'required',
'type' => 'required',
'value' => 'required|numeric',
'cart_value' => 'required|numeric',
'expiry_date' => 'required'
]);
$coupon = Coupon::find($this->coupon_id);
$coupon->code = $this->code;
$coupon->type = $this->type;
$coupon->value = $this->value;
$coupon->cart_value = $this->cart_value;
$coupon->expiry_date = $this->expiry_date;
$coupon->save();
session()->flash('message','Coupon has been updated successfully!');
}

Now, it's done, so let's check this coupon edit.

So, switch to the browser and refresh the page.

Now, click on the edit link.

And you can see here the expiry date.

Now, change the date and select any date.

Now, click on update, and you can see here that the coupon has been updated successfully.

Now, let's implement this expiry date with the coupon on the cart.

So, go to the CartComponent.php class file.

And inside the applyCouponCode function, just add the following code:


public function applyCouponCode()
{
$coupon = Coupon::where('code',$this->couponCode)->where('expiry_date','>=',Carbon::today())->where('cart_value','<=',Cart::instance('cart')->subtotal())->first();
if(!$coupon)
{
session()->flash('coupon_message','Coupon code is invalid!');
return;
}

session()->put('coupon',[
'code' => $coupon->code,
'type' => $coupon->type,
'value' => $coupon->value,
'cart_value' => $coupon->cart_value
]);

}

Now, it's done, so save this file and let's check it.

So, switch to the browser and refresh the page.

And let's use this coupon, you can see here that the expire date is 4 March and today is 3 March, it means this coupon is valid.

So, let's apply this coupon.

Check this and enter here the coupon code.

And you can see that the coupon has been applied.

Now, remove the coupon.

And let's change the expiry date of the coupon.

And set any expired date.

Now, update this.

Now, go to the cart page and let's enter the coupon code and click on apply.

You can see here an error message: "Coupon is invalid".

So, in this way, you can add an expiry date to coupons.