NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages
Coupons for Laravel

Query scopes & route binding

use RoundlyConsulting\Coupons\Models\Coupon;

Coupon::query()->active()->get();
Coupon::query()->expired()->get();
Coupon::query()->exhausted()->get();
Coupon::query()->redeemable()->get();    // active AND not expired AND not at max usage
Coupon::query()->whereCode('SAVE20')->first();

Coupon::query()->expired()->onlyTrashed()->count();   // scopes combine with soft deletes
ScopeMatches
active()activated_at is set and <= now.
expired()expires_at is set and <= now.
exhausted()max_usage > 0 and usage >= max_usage.
redeemable()Active, not expired and not exhausted.
whereCode(string $code)The coupon holding the code — case-insensitive, surrounding whitespace trimmed; a blank code matches nothing.

Coupons::redeemable() returns the same redeemable() query through the configured model.

Route-model binding

{coupon} route parameters bind by code by default — getRouteKeyName() returns coupons.route_key — and match it case-insensitively, like every lookup. Set it to id to bind by primary key:

use Illuminate\Support\Facades\Route;
use RoundlyConsulting\Coupons\Models\Coupon;

Route::get('/coupons/{coupon}', fn (Coupon $coupon) => $coupon);   // /coupons/SAVE20 — or /coupons/save20

// .env — bind by primary key instead: /coupons/42
// COUPONS_ROUTE_KEY=id

Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

More ways to support, including crypto

By donating, you agree to our donation terms.

Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.