---
title: "Query scopes & route binding — Coupons for Laravel | Roundly"
description: "Find active, expired, exhausted and redeemable coupons with query scopes, and bind {coupon} route parameters by code or by id."
url: https://roundly-consulting.com/open-source/docs/coupons-for-laravel/query-scopes
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Coupons for Laravel](https://roundly-consulting.com/open-source/docs/coupons-for-laravel.md)

# Query scopes & route binding

```php
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
```

| Scope | Matches |
| --- | --- |
| `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:

```php
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](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## 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.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/coupons-for-laravel.md)
