Laravel 8 E-Commerce - Admin Create Coupons
In this video, we are going to learn about creating coupons.
So, let's see how we can create coupons.
For coupons, first of all, let's create a new model.
So, switch to the command prompt and run the command to create a new model:
php artisan make:model Coupon –m
Now, switch to the project and let's open the create_coupons_table migration file 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 let's migrate the migration. So, switch to the command prompt and run the command:
php artisan migrate
Now, let's create three Livewire components for coupons:
First, a component for showing all the coupons. Second, a component for adding a new coupon. And third, a component for editing a coupon.
So, for creating Livewire components, 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 let's create the routes for these three components.
So, go inside the routes directory and open the web.php file.
Now, inside the admin middleware group, let's 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 the 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 the 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, let's 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 the 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, let's 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 add 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, let's check it.
So, switch to the project and now go to the admin menu and let's click on the All Coupons link.
And here, you can see the coupon table.
Now, let's add a coupon.
So, click on the Add Coupon link.
Now, enter 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 any coupon.
And also, you can delete a coupon from here.
So, in this way, you can create coupons.
In the next tutorial, we will see how to apply these coupons.