Laravel 11 E-Commerce - Admin Edit Coupon

Welcome back to the Laravel E-Commerce Project Tutorial. In this video, we will learn about creating an admin edit coupon feature.

Step 1: Create a Function in the AdminController

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


public function edit_coupon($id)
{
$coupon = Coupon::find($id);
return view('admin.coupon-edit',compact('coupon'));
}

Step 2: Create a Route for the Function

Next, create a route for this function:


Route::put('/admin/coupon/update',[AdminController::class,'update_coupon'])->name('admin.coupon.update');

Step 3: Create a View for Editing a Coupon

Now, go to the resources directory and create a new file called coupon-edit.blade.php. Open the coupon-add.blade.php file and copy all the content from there, then paste it inside the coupon-edit.blade.php file. Make 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">Edit 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.update')}}" >
@csrf
@method("put")
<input type="hidden" name="id" value="{{$coupon->id}}" />
<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="{{$coupon->code}}" aria-required="true" required="">
</fieldset>
<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" {{$coupon->type=='fixed'? 'selected':''}}>Fixed</option>
<option value="percent" {{$coupon->type=='percent'? 'selected':''}}>Percent</option>
</select>
</div>
</fieldset>
<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="{{$coupon->value}}" aria-required="true" required="">
</fieldset>
<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="{{$coupon->cart_value}}" aria-required="true" required="">
</fieldset>
<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="{{$coupon->expiry_date}}" aria-required="true" required="">
</fieldset>

<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 4: Create a Function for Updating the Coupon

Now, go to the AdminController and create a new function for updating the coupon:


public function update_coupon(Request $request)
{
$request->validate([
'code' => 'required',
'type' => 'required',
'value' => 'required|numeric',
'cart_value' => 'required|numeric',
'expiry_date' => 'required|date'
]);

$coupon = Coupon::find($request->id);
$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 updated successfully !');
}

Step 5: Create a Route for Updating the Coupon

Next, create a route for this update_coupon function:


Route::put('/admin/coupon/update',[AdminController::class,'update_coupon'])->name('admin.coupon.update');

Step 6: Update the Coupon Edit View

Now, go to the coupon-edit.blade.php file and set the form method and action:

method="POST" action="{{route('admin.coupon.update')}}"

Also, add the @method directive and pass the method name:

@method("PUT")

Step 7: Add an Edit Link

Finally, add an edit link. Go to the coupons.blade.php file and inside the foreach loop, add the edit link:


<td>
<div class="list-icon-function">
<a href="{{route('admin.coupon.edit',['id'=>$coupon->id])}}">
<div class="item edit">
<i class="icon-edit-3"></i>
</div>
</a>
</div>
</td>

Now it's done! Let's check it out. Go to the /admin/coupons page, edit a coupon, make some changes, and click on update. You can see that the coupon has been updated.

In this way, you can edit a coupon.

That's all about editing a coupon.