In this video we are going to learn about Create Coupons.
So let see how can we create coupons.
For Coupons first of all lets create a new model.
So switch to command prompt and for creating new model lets run the command.


php artisan make:model Coupon –m


Now switch to the project and lets open the create_coupons_table migration and write the following code.

<?php

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

class CreateCouponsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('coupons', function (Blueprint $table) {
$table->id();
$table->string('code')->unique();
$table->enum('type',['fixed','percent']);
$table->decimal('value');
$table->decimal('cart_value');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('coupons');
}
}


Alright, Now lets migrate the migration so switch to command prompt and run the command.


php artisan migrate


Now lets create three livewire component for Coupons.
First component for showing all the coupons second component for add new coupon and third component for edit coupon.
So for creating livewire componenst run the command.


php artisan make:livewire admin/AdminCouponsComponent
Php artisan make:livewire admin/AdminAddCouponComponent
Php artisan make:livewire admin/AdminEditCouponComponent



Now switch to the project and lets create the route for these three Component.
So go inside the routes directory then open web.php file.
Now inside the admin middleware group lets create the route.


Route::get('/admin/coupons',AdminCouponsComponent::class)->name('admin.coupons');
Route::get('/admin/coupon/add',AdminAddCouponComponent::class)->name('admin.addcoupon');
Route::get('/admin/coupon/edit/{coupon_id}',AdminEditCouponComponent::class)->name('admin.editcoupon');  


Now open AdminCouponsComponent.php class file and write the following code.


<?php

namespace App\\Http\\Livewire\\Admin;

use App\\Models\\Coupon;
use Livewire\\Component;

class AdminCouponsComponent extends Component
{

public function deleteCoupon($coupon_id)
{
$coupon = Coupon::find($coupon_id);
$coupon->delete();
session()->flash('message','Coupon has been deleted successfully!');
}

public function render()
{
$coupons = Coupon::all();
return view('livewire.admin.admin-coupons-component',['coupons'=>$coupons])->layout('layouts.base');
}
}


Now open admin-coupon-component.blade.php view file and write the following code.


<div>
<div class=\"container\" style=\"padding:30px 0;\">
<div class=\"row\">
<div class=\"col-md-12\">
<div class=\"panel panel-default\">
<div class=\"panel-heading\">
<div class=\"row\">
<div class=\"col-md-6\">
All Coupon
</div>
<div class=\"col-md-6\">
<a href=\"{{route('admin.addcoupon')}}\" class=\"btn btn-success pull-right\">Add New</a>
</div>
</div>
</div>
<div class=\"panel-body\">
@if(Session::has('message'))
<div class=\"alert alert-success\" role=\"alert\">{{Session::get('message')}}</div>
@endif
<table class=\"table table-striped\">
<thead>
<tr>
<th>Id</th>
<th>Coupon Code</th>
<th>Coupon Type</th>
<th>Coupon Value</th>
<th>Cart Value</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($coupons as $coupon)
<tr>
<td>{{$coupon->id}}</td>
<td>{{$coupon->code}}</td>
<td>{{$coupon->type}}</td>
@if($coupon->type == 'fixed')
<td>${{$coupon->value}}</td>
@else
<td>{{$coupon->value}} %</td>
@endif
<td>${{$coupon->cart_value}}</td>
<td>
<a href=\"{{route('admin.editcoupon',['coupon_id'=>$coupon->id])}}\"><i class=\"fa fa-edit fa-2x\"></i></a>
<a href=\"#\" onclick=\"confirm('Are you sure, You want to delete this coupon?') || event.stopImmediatePropagation()\" wire:click.prevent=\"deleteCoupon({{$coupon->id}})\" style=\"margin-left:10px; \"><i class=\"fa fa-times fa-2x text-danger\"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>


Now lets open the AdminAddCouponComponent.php class file and write the following code.


<?php

namespace App\\Http\\Livewire\\Admin;

use App\\Models\\Coupon;
use Livewire\\Component;

class AdminAddCouponComponent extends Component
{

public $code;
public $type;
public $value;
public $cart_value;

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

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

public function render()
{
return view('livewire.admin.admin-add-coupon-component')->layout('layouts.base');
}
}


Now open admin-add-coupon-component.blade.php file and write the following code.


<div>
<div class=\"container\" style=\"padding: 30px 0;\">
<div class=\"row\">
<div class=\"col-md-12\">
<div class=\"panel panel-default\">
<div class=\"panel-heading\">
<div class=\"row\">
<div class=\"col-md-6\">
Add New Coupon
</div>
<div class=\"col-md-6\">
<a href=\"{{route('admin.coupons')}}\" class=\"btn btn-success pull-right\">All Coupon</a>
</div>
</div>
</div>
<div class=\"panel-body\">
@if(Session::has('message'))
<div class=\"alert alert-success\" role=\"alert\">{{Session::get('message')}}</div>
@endif
<form class=\"form-horizontal\" wire:submit.prevent=\"storeCoupon\">
<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Coupon Code</label>
<div class=\"col-md-4\">
<input type=\"text\" placeholder=\"Coupon Code\" class=\"form-control input-md\" wire:model=\"code\" />
@error('code') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>
<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Coupon Type</label>
<div class=\"col-md-4\">
<select class=\"form-control\" wire:model=\"type\">
<option value=\"\">Select</option>
<option value=\"fixed\">Fixed</option>
<option value=\"percent\">Percent</option>
</select>
@error('type') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>

<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Coupon Value</label>
<div class=\"col-md-4\">
<input type=\"text\" placeholder=\"Coupon Value\" class=\"form-control input-md\" wire:model=\"value\" />
@error('value') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>

<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Cart Value</label>
<div class=\"col-md-4\">
<input type=\"text\" placeholder=\"Cart Value\" class=\"form-control input-md\" wire:model=\"cart_value\" />
@error('cart_value') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>

<div class=\"form-group\">
<label class=\"col-md-4 control-label\"></label>
<div class=\"col-md-4\">
<button type=\"submit\" class=\"btn btn-primary\">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>




Now lets open the AdminEditCouponComponent.php class file and write the following code.


<?php

namespace App\\Http\\Livewire\\Admin;

use App\\Models\\Coupon;
use Livewire\\Component;

class AdminEditCouponComponent extends Component
{
public $code;
public $type;
public $value;
public $cart_value;
public $coupon_id;

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;
}

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

]);
}

public function updateCoupon()
{
$this->validate([
'code' => 'required',
'type' => 'required',
'value' => 'required|numeric',
'cart_value' => 'required|numeric'
]);
$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->save();
session()->flash('message','Coupon has been updated successfully!');
}

public function render()
{
return view('livewire.admin.admin-edit-coupon-component')->layout('layouts.base');
}
}


Now open the admin-edit-coupon-component.blade.php file and write the following code.


<div>
<div class=\"container\" style=\"padding: 30px 0;\">
<div class=\"row\">
<div class=\"col-md-12\">
<div class=\"panel panel-default\">
<div class=\"panel-heading\">
<div class=\"row\">
<div class=\"col-md-6\">
Edit Coupon
</div>
<div class=\"col-md-6\">
<a href=\"{{route('admin.coupons')}}\" class=\"btn btn-success pull-right\">All Coupon</a>
</div>
</div>
</div>
<div class=\"panel-body\">
@if(Session::has('message'))
<div class=\"alert alert-success\" role=\"alert\">{{Session::get('message')}}</div>
@endif
<form class=\"form-horizontal\" wire:submit.prevent=\"updateCoupon\">
<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Coupon Code</label>
<div class=\"col-md-4\">
<input type=\"text\" placeholder=\"Coupon Code\" class=\"form-control input-md\" wire:model=\"code\" />
@error('code') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>
<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Coupon Type</label>
<div class=\"col-md-4\">
<select class=\"form-control\" wire:model=\"type\">
<option value=\"\">Select</option>
<option value=\"fixed\">Fixed</option>
<option value=\"percent\">Percent</option>
</select>
@error('type') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>

<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Coupon Value</label>
<div class=\"col-md-4\">
<input type=\"text\" placeholder=\"Coupon Value\" class=\"form-control input-md\" wire:model=\"value\" />
@error('value') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>

<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Cart Value</label>
<div class=\"col-md-4\">
<input type=\"text\" placeholder=\"Cart Value\" class=\"form-control input-md\" wire:model=\"cart_value\" />
@error('cart_value') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>

<div class=\"form-group\">
<label class=\"col-md-4 control-label\"></label>
<div class=\"col-md-4\">
<button type=\"submit\" class=\"btn btn-primary\">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>




Now go to the base.blade.php layout file and the Coupons link inside the admin menu.


<li class=\"menu-item\">
<a title=\"All Coupon\" href=\"{{route('admin.coupons')}}\">All Coupon</a>
</li>


Now all done so lets check it.
So switch to the project and now go to the admin menu and lets click on all coupons link And here you can see the coupon table.
Now lets add a coupon.
So click on add coupn link.
Now enter the any coupon code, type, value and cart_value on which this coupon code is valid.
Now click on submit.
And here you can see the coupon.
Now add one more coupon.
And from here you can edit the any coupon.
And also you delete coupon from here.
So in this way you can Create Coupons.
In next tutorial we will see how can we apply this Coupons.