Laravel 12 E-Commerce Project Tutorial

Admin Create Coupons Page

Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. In this video, we will be learning how to create the Admin Coupons Page. Let'swalk through the steps to create this page.

  1. Create Coupon Model and Migration

    First, we need to create the Coupon Model and its corresponding migration.

    Run the following command in your command prompt:

    public function coupons()
    {
    $coupons = Coupon::orderBy("expiry_date","DESC")->paginate(12);
    return view("admin.coupons",compact("coupons"));
    }

    This command will successfully create both the model and the migration file.

  2. Create the AdminController Function

    Next, let'screate a new function within the AdminController to handle the display of all coupons:

    Route::get("/admin/coupons",[AdminController::class,"coupons"])->name("admin.coupons");
  3. Define the Route

    Now, define the route for accessing the admin coupons page:

    <x-admin-layout>
    </x-admin-layout>
  4. Create the View File

    Go to the resources/views/admin directory and create a new view file named coupons.blade.php.

    In this file, extend the admin layout:

     
    <tbody>
    @foreach ($coupons as $coupon)
    <tr>
    <td scope="row">{{$coupon->id}}</td>
    <td>{{$coupon->code}}</td>
    <td>{{$coupon->type}}</td>
    <td>{{$coupon->value}}</td>
    <td>{{$coupon->cart_value}}</td>
    <td>{{$coupon->expiry_date}}</td>
    <td>
    <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>
    <form action="{{route("admin.coupon.delete",["id"=>$coupon->id])}}" method="POST">
    @csrf
    @method("DELETE")

    <button type="submit" style="background:none; border:none; padding:0;">
    <div class="item text-danger delete" bis_skin_checked="1">
    <i class="fa fa-trash"></i>
    </div>
    </button>
    </form>
    </div>

    </td>
    </tr>
    @endforeach
    </tbody>
    </table>
    {{-- Note: The original code had $categories->links(), which should be $coupons->links() --}}
    {{$coupons->links("pagination::bootstrap-5")}}
    </div>

    Then, copy the main content structure (the div with the class main-content) from your coupons.html template file and paste it inside the coupons.blade.php layout section.

    Ensure you add the dashboard link using {{route("admin.index’)}} where appropriate in the view.

  5. Displaying Coupons Data in the View

    Inside the view, implement the loop to display the coupon data retrieved from the controller. The table body should look like this:

    pacehoder_5
  6. Update Admin Layout

    Finally, open your admin layout file and add the link for the coupons page using {{ route("admin.coupons") }} in the navigation menu.

Verification

After completing these steps, go to your admin page and click on the Coupons link. You should now see the coupons page. Since no coupons are currently stored in the table, the list will be empty. In the next video, we will cover how to add a new coupon.


This concludes the process for creating the Admin Coupons Page.