Blog Detail


Add Polymorphic Coupon Functionality in Your Laravel App cover image

arrow_back Add Polymorphic Coupon Functionality in Your Laravel App

Laravel Couponables is an amazing package that provides polymorphic coupon functionality for your Laravel application. The package requires PHP ^8.x and Laravel ^8.71 or ^9.0.


You can install this package using composer:

composer require michael-rubel/laravel-couponables

Publish the migrations:

php artisan vendor:publish --tag="couponables-migrations"

Publish the config file:

php artisan vendor:publish --tag="couponables-config"


After publishing migrations, apply a trait in the model you want to use as a $redeemer:

use HasCoupons;

Artisan command

You can add coupons to your database using the Artisan command:

php artisan make:coupon YourCouponCode

Optionally, you can pass the next arguments:

'--value'         // The 'value' to perform calculations based on the coupon provided
'--type'          // The 'type' to point out calculation strategy
'--limit'         // Limit how many times the coupon can be applied by the model
'--quantity'      // Limit how many coupons are available overall (this value will decrement)
'--expires_at'    // Set expiration time for the coupon
'--redeemer_type' // Polymorphic model type. Can as well be morph-mapped value, i.e. 'users'
'--redeemer_id'   // Redeemer model ID
'--data'          // JSON column to store any metadata you want for this particular coupon

Adding coupons using model

You can as well add coupons simply using the model:

    'code'  => '...',
    'value' => '...',

Basic operations

Verify the coupon code:


Redeem the coupon:


Redeem the coupon in the context of another model:


Combined redeemCoupon and for behavior (assuming the $course includes HasCoupons trait):

If something’s going wrong, methods verifyCoupon and redeemCoupon will throw an exception:

CouponExpiredException      // Coupon is expired (`expires_at` column).
InvalidCouponException      // Coupon is not found in the database.
NotAllowedToRedeemException // Coupon is assigned to the specific model (`redeemer` morphs).
OverLimitException          // Coupon is over the limit for the specific model (`limit` column).
OverQuantityException       // Coupon is exhausted (`quantity` column).
CouponException             // Generic exception for all cases.

For more details, You can visit its documentation and source code on Github.


Being a custom web development company, Codebrisk has a lot of experience in developing CRM software, and e-commerce and SASS web applications for a variety of industries. So if you want to develop any web application, We are here to assist you with this initiative. Please feel free to get in touch with us or you can get a free quote here.

Published at : 23-06-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project