In this video we are going to learn about Add Coupons Expiry Date.
So let see how can we add expiry date to coupons.
First of lets add a column inside the coupons table.
For that lets create a migration for adding a column.
So switch to the command prompt, and for creating a migration just run the command.
Now switch to the project and lets open the migration.
Inside the add_expiry_date_to_coupons_table migration add the following code.
Now lets migrate this migration.
In command prompt run the command.
Alright now run the application
Now switch to the project and lets open admin-coupons-component.blade.php view file.
Here add a new column inside the table.
And inside the table body add the following td.
Now save it and lets check this.
So switch to browser and refresh the page.
Now you can see here expire date column.
Now click on add new coupon and here inside this form lets add a text box for expiry date input.
So switch to the project and lets open the admin-add-coupon-component.blade.php file
And lets create add a text field here for expiry date.
After this in this view file on page bottom add the following code.
Now go to AdminAddCouponComponent.php Class File
And lets create a property.
Now inside the updated and storeCoupon function make following changes.
Now save it and lets check it.
So switch to the browser and refresh the page.
Now lets add a new coupon.
And enter here coupon code now select the expiry date now click on submit.
You can see coupon has been created.
Now lets add this expiry date on edit coupon component.
So for that go to the admin-edit-coupon-component.blade.php view file
And here add the following code.
And add the following code on page bottom.
Now lets open the AdminEditCouponComponent.php class file
And here lets create the property.
Now inside the mount, updated and updateCoupon method make following changes.
Now its done so lets check this coupon edit.
So switch to browser and refresh the page.
Now click on 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 coupon updated successfuly.
Now lets implement this expiry date with coupon on cart.
So go to the CartComponent.php class file.
And inside the applyCouponCode function just add the following code.
Now its done so save this file and lets check it.
So switch to the browser and refresh the page.
And lets use this coupon, you can see here the expire date is 4 march and today is 3 march it means this coupon is valid.
So lets apply this coupon.
Check this and enter here the coupon code.
And you can see coupons has been applied.
Now remove the coupon.
And lets change the expiry date of coupon.
And set the any expired date.
Now update this.
Now go to the cart page lets enter the coupon code and click on apply.
you can see here error message coupon is invalid.
So in this way you can Add Coupons Expiry Date.
So let see how can we add expiry date to coupons.
First of lets add a column inside the coupons table.
For that lets 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 lets 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 lets migrate this migration.
In command prompt run the command.
php artisan migration
Alright now run the application
php artisan serve
Now switch to the project and lets open 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 lets check this.
So switch to browser and refresh the page.
Now you can see here expire date column.
Now click on add new coupon and here inside this form lets add a text box for expiry date input.
So switch to the project and lets open the admin-add-coupon-component.blade.php file
And lets create add 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 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 AdminAddCouponComponent.php Class File
And lets create a property.
public $expiry_date
Now inside the updated and storeCoupon function make 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 lets check it.
So switch to the browser and refresh the page.
Now lets add a new coupon.
And enter here coupon code now select the expiry date now click on submit.
You can see coupon has been created.
Now lets add this expiry date on 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 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 lets open the AdminEditCouponComponent.php class file
And here lets create the property.
Now inside the mount, updated and updateCoupon method make 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 its done so lets check this coupon edit.
So switch to browser and refresh the page.
Now click on 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 coupon updated successfuly.
Now lets implement this expiry date with coupon on 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 its done so save this file and lets check it.
So switch to the browser and refresh the page.
And lets use this coupon, you can see here the expire date is 4 march and today is 3 march it means this coupon is valid.
So lets apply this coupon.
Check this and enter here the coupon code.
And you can see coupons has been applied.
Now remove the coupon.
And lets change the expiry date of coupon.
And set the any expired date.
Now update this.
Now go to the cart page lets enter the coupon code and click on apply.
you can see here error message coupon is invalid.
So in this way you can Add Coupons Expiry Date.