Releasing new features to a specific group of users or gradually is an important practice in modern projects. Laravel handles this elegantly using Laravel Pennant — an official solution for feature flags that is simple, powerful, and well integrated into the ecosystem.
In this post, you will learn how to:
- install laravel pennat
- create and manage feature flags
- Use flags by env, by user, and more
Installation
Install the package via composer:
composer require laravel/pennant
Publish the config(optional):
php artisan vendor:publish --tag=pennant-config
Creating a Feature Flag
You can create a flag using the command:
php artisan pennant:feature new-dashboard
This will create a class in app/Features/NewDashboard.php
. It looks like this.
namespace App\Features;
use Laravel\Pennant\Feature;
class NewDashboard
{
public function resolve(mixed $scope): bool
{
return true;
}
}
The resolve()
function decides if the feature is active for the current "scope" usually, an authenticated user.
Enabling by user
Want to release it only for some users ?
public function resolve(mixed $scope): bool
{
return $scope->email === '[email protected]';
}
Or based on roles, Ids, etc.
Enabling globally
To release it for everyone:
Feature::activate('new-dashboard');
To deactivate:
Feature::deactivate('new-dashboard');
Using it in code
You can check if the feature is active like this:
use Illuminate\Support\Facades\Feature;
if (Feature::active('new-dashboard')) {
// show new dashboard
}
Or with a scope:
if (Feature::for($user)->active('new-dashboard')) {
// show only for this user
}
Common use cases
Gradual deploy (e.g. 10% of users)
Beta features for a specific group
A/B Testing
Security flag to turn something off fast
Final Tip: use feature migrations wisely
You can store flags in the database with the database
driver, which is useful when you need to persist the flag state.
// config/pennant.php
'driver' => 'database',
Run:
php artisan pennant:table
php artisan migrate
Conclusion
Laravel Pennant gives you a light, expressive, and official way to work with feature flags, letting you deliver features with confidence, safety, and control.
If you're not using feature flags in your project yet, now is the perfect time to start!