Feature Flags in Laravel 13 with Pennant: Deploy Without Surprises and Retire Them on Time
A feature flag is not an if statement with extra steps: it is a decision you can reverse without deploying. Pennant brings that to Laravel 13 with no external service, plus one catch nobody mentions on day one: the value it resolves gets stored, and it stays stored.
Installing It in a Laravel 13 Project
Three Commands and One Table
The package is first-party and installs like any other dependency:
composer require laravel/pennant
Next you publish config and migration, then migrate. That creates the features table, where the default driver keeps resolved values:
Read also
php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
php artisan migrate
If you prefer to publish the pieces separately, the package repo exposes the pennant-config and pennant-migrations tags. On a fresh project there is nothing else to do: no service, no credentials, no extra queue.
The Two Drivers: database and array
Configuration lands in config/pennant.php, and that is where you pick storage. database is the default and uses the table you just created. array keeps values in memory for the request: nothing persists, which is exactly what you want in tests or single-shot processes.
Defining a Flag and Deciding Who Gets It
Closure Definitions in a Service Provider
A flag is a named rule. The shortest definition lives in a service provider's boot method:
Feature::define('new-api', fn (User $user) => $user->team->plan === 'pro');
When the logic grows, use a dedicated class: php artisan pennant:feature NewApi scaffolds one in app/Features with its own resolve method. Pennant also supports externally discovered definitions, which helps when a package ships its own flags.
Scope: Global, User, Team
By default a flag resolves for the authenticated user. You can move it with Feature::for($user), or, when the decision belongs to the team rather than the person, with Feature::for($user->team). The docs also cover global scope, nullable scope and a custom scope resolver, which is the way out when your app does not use the default guard.
Rich Values: Not Everything Is True or False
A flag does not have to return a boolean. It can return a variant name and let the rest of the code decide:
Feature::activate('purchase-button', 'seafoam-green');
$variant = Feature::value('purchase-button');
That makes Pennant usable for picking layout or copy, as long as you accept what it is not: a distribution and kill switch, not an experiment platform. It does not measure conversion on its own.
Checking the Flag: PHP, Blade and Routes
Feature::active, inactive and value
In PHP, three methods cover almost everything: Feature::active('new-api'), Feature::inactive('new-api') and Feature::value('new-api') when the flag returns more than a boolean. Note that inactive is not "undefined": an unknown flag fails the check, so the definition has to be loaded first.
@feature and @noFeature in Blade
In views the package registers two directives, which keeps templates readable and controllers free of extra variables:
@feature('new-api') ... @endfeature
@noFeature('new-api') ... @endnoFeature
Guarding a Whole Route
When the feature sits behind its own routes, you do not need to check in every controller. The package ships the EnsureFeaturesAreActive middleware, applied through ::using('new-api'), and it accepts whenInactive() so you can return your own response when the flag is off.
Gradual Rollout: Percentages and the Stored-Value Catch
How the Lottery Picks Who Gets In
To turn a feature on for a percentage of users, the definition returns a lottery tied to the scope:
Feature::define('new-api', fn (User $user) => Lottery::odds(1, 10));
The key detail is that the lottery depends on the scope, not on per-request randomness: the same user always lands on the same side. With rich values you can split variants using winner() and loser(), for example two versions of a button.
Why Raising the Percentage Is Not Enough
Here is the catch. The first time a flag value resolves, the driver stores it, and later requests read it from storage. That is what gives users a consistent experience, and also what breaks the intuition of a gradual rollout: if someone resolved at 10 percent and landed outside, raising the lottery to 50 percent will not let them in, because their value is already written.
To force a fresh resolution you have to delete the stored value. Feature::forget('new-api') forgets the current scope; Feature::purge('new-api') clears every scope for that flag, and Feature::purge() clears everything. From the command line, the same thing:
php artisan pennant:purge new-api
The command takes several names at once, --except= to keep specific flags and --except-registered to clear everything no longer defined in a service provider. Watch the meaning: purging does not switch the feature off in code, it only deletes values, and the next check re-applies whatever definition you have deployed.
Turning It On and Off for Everyone
For a hard stop there are two methods: Feature::activateForEveryone('new-api') and Feature::deactivateForEveryone('new-api'), the first one taking an optional rich value. They update stored values across every scope but do not touch the code definition: if that definition returns false on its own logic, the bulk value can resolve again later. The kill switch works; forgetting to change the definition is what leaves zombie flags behind.
Testing Flags Without Dirtying the Database
The array Driver in Tests
In tests you do not want the features table in the way. With the array driver values live in memory and vanish when the test ends, so every case starts clean with no migrations and no manual cleanup.
One Active Case and One Off Case
Define the flag inside the test with the condition you want to prove, then assert both branches: one where the condition holds and one where it does not. If the flag gates a route, the test should exercise the middleware, not just the model method. That is the minimum that avoids the classic "it worked on my machine".
Operations: Queries, Events and Retiring the Flag
Feature::load and the N+1 Problem
With the database driver, every check can hit the database. If you are rendering a hundred users with their flag state, do not check them one at a time: Feature::load(['new-api', 'purchase-button']) resolves several flags at once, and Feature::for($users)->load([...]) does the same for a collection of scopes.
Events: Who Changed What
Pennant fires events on activation and deactivation: FeatureUpdated for a single scope and FeatureUpdatedForAllScopes for bulk changes. Listening to them and writing a log line is the cheap way to answer the question that always shows up after an incident: who turned this off, and when.
When to Retire a Flag and How to Do It Safely
The hard part of a feature flag is not turning it on: it is turning it off and deleting it. The order that breaks nothing: first activateForEveryone with the winning behavior and a week where nobody switches it off; then remove the check from the code and the dead branch; finally delete the definition and purge with php artisan pennant:purge new-api. Start with the definition instead and users holding a stored value keep seeing the old behavior while the code no longer knows about it.
Conclusion: The Flag Lives Shorter Than the Feature
Pennant solves deployment with no external service and three commands, but its real value is operational: switching a feature off in a minute without touching the pipeline, and knowing the stored value is part of the contract. If you are coming from there, our pieces on what is new in Laravel 13, gates and policies and deploying on a VPS complete the picture of an app you can change without surprises.

