Laravel 12 E-Commerce Project Tutorial

Admin: Editing an Existing Coupon

Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. This video focuses on implementing the functionality for Admin Edit Coupon. Let'swalk through the necessary steps.

  1. Create the Edit Function in AdminController

    Open your AdminController and create the edit_coupon function, which accepts the coupon $id, finds the corresponding coupon record, and loads the edit view:

    public function edit_coupon($id)
    {
    $coupon = Coupon::find($id);
    return view("admin.coupon-edit",compact("coupon"));
    }
  2. Define the Route for the Edit View

    Define the route that includes the coupon ID parameter for accessing the edit form:

    Route::get("/admin/coupon/edit/{id}",[AdminController::class,"edit_coupon"])->name("admin.coupon.edit");
  3. Create the Coupon Edit View (coupon-edit.blade.php)

    Create a new file named coupon-edit.blade.php in the resources/views/admin directory. For convenience, copy all the content from the coupon-add.blade.php file and paste it here.

    Now, make the following required changes to populate the form with existing data and set up the update action:

    Form Setup and Data Population

    <div class="card-body">
    <form method="POST" action="{{ route("admin.coupon.update") }}" data-parsley-validate>
    @csrf
    @method("PUT")
    <input type="hidden" name="id" value="{{ $coupon->id }}" />
    <div class="row mb-3">
    <div class="col-md-6">
    <label class="form-label" for="code">Coupon Code</label>
    <input type="text" class="form-control" name="code" value="{{ $coupon->code }}" placeholder="" required />
    @error("code")<span class="alert alert-danger text-center">{{ $message }}</span>@enderror
    </div>
    <div class="col-md-6">

    <label class="form-label" for="type">Coupon Type</label>
    <select class="form-select" name="type" required>
    <option value="">--Select Coupon Type</option>
    <option value="fixed" {{ ($coupon->type == "fixed" ?
    "selected" : "") }}>Fixed Amount</option>
    <option value="percent" {{ ($coupon->type == "percent" ? "selected" : "") }}>Percentage</option>
    </select>
    @error("type")<span class="alert alert-danger text-center">{{ $message }}</span>@enderror
    </div>
    </div>
    <div class="row mb-3">
    <div class="col-md-6">
    <label class="form-label" for="value">Value</label>
    <input type="text" class="form-control" name="value" value="{{ $coupon->value }}" placeholder="" required />
    @error("value")<span class="alert alert-danger text-center">{{ $message }}</span>@enderror
    </div>

    <div class="col-md-6">
    <label class="form-label" for="cart_value">Cart Value</label>
    <input type="text" class="form-control" name="cart_value" value="{{ $coupon->cart_value }}" placeholder="" required />
    @error("cart_value")<span class="alert alert-danger text-center">{{ $message }}</span>@enderror
    </div>
    </div>
    <div class="row mb-3">
    <div class="col-md-6">
    <label class="form-label" for="expiry_date">Expiry Date</label>
    <input type="date" class="form-control" name="expiry_date" value="{{ $coupon->expiry_date }}" required />
    @error("expiry_date")<span class="alert alert-danger text-center">{{ $message }}</span>@enderror
    </div>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
    </form>
    </div>
  4. Create the Coupon Update Function

    In AdminController, create the update_coupon function to handle the form submission. This function validates the input, finds the existing coupon using the hidden `id` field, updates the attributes, and saves the changes.

    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();

    session()->flash("record_deleted", "task was successful!");
    return redirect()->route("admin.coupons");
    }
  5. Define the Update Route

    Create a PUT route for handling the update logic:

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

  6. Add the Edit Link to the Coupons List

    Finally, open coupons.blade.php and update the action column within the @foreach loop to include the edit link:

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

Verification 🚀

Go to the /admin/coupons page, click on the Edit link for any coupon, make changes to the form, and click Submit. You should be redirected back to the coupon list, and the coupon details will be updated.


This concludes the process for editing an admin coupon.