Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What methods are available to activate or deactivate feature flags in Laravel Pennant


What methods are available to activate or deactivate feature flags in Laravel Pennant


Laravel Pennant provides several methods to activate or deactivate feature flags, allowing fine-grained control over feature availability in your application. Here are the detailed ways to manage feature flags in Laravel Pennant:

Defining Feature Flags

Feature flags are defined using the `Feature::define()` method, typically in a service provider or a dedicated place in your code. You can define simple flags or conditional flags based on user attributes or other logic.

php
use Laravel\Pennant\Feature;

Feature::define('telecommunications', fn () => config('features.telecommunications.enabled'));

Or with conditions based on the user:

php
Feature::define('telecommunications', fn (User $user) => $user->isAdmin());

Checking Feature Flags

Before activating or deactivating, you often check whether a feature is active using:

- `Feature::active('feature-name')` - checks if a feature is active for the current user or scope.
- `Feature::inactive('feature-name')` - checks if a feature is inactive.
- `Feature::for($user)->active('feature-name')` - checks if a feature is active for a specific user or scope.

Activating Feature Flags

- Activate for the current user or default scope:**

php
Feature::activate('telecommunications');

- Activate for a specific user or scope:**

php
$anotherUser = User::find(2);
Feature::for($anotherUser)->activate('telecommunications');

- Activate for everyone (globally):**

php
Feature::activateForEveryone('telecommunications');

Deactivating Feature Flags

- Deactivate for the current user or default scope:**

php
Feature::deactivate('telecommunications');

- Deactivate for a specific user or scope:**

php
$anotherUser = User::find(2);
Feature::for($anotherUser)->deactivate('telecommunications');

- Deactivate for everyone (globally):**

php
Feature::deactivateForEveryone('telecommunications');

Additional Details

- By default, feature flags are scoped to the authenticated user, but this can be changed using the `Feature::for()` method to target other users or scopes.
- Feature flags can be stored in-memory or persisted in a database, depending on the configured driver (`array` or `database`).
- Using these methods, you can implement gradual rollouts, beta testing for specific user groups, or emergency feature toggling in case of bugs.

These methods provide a flexible and centralized way to manage feature availability dynamically in Laravel applications using Pennant[2][4][6].

Citations:
[1] https://laravel.com/docs/12.x/pennant
[2] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[3] https://laravel-news.com/feature-flags-with-laravel-pennant
[4] https://dev.to/saurabh-dhariwal/how-to-manage-feature-flags-with-laravel-pennant-in-2024-1phb
[5] https://nedoto.com/2024/07/14/laravel-feature-flag/
[6] https://dev.to/aleson-franca/laravel-pennant-releasing-features-with-feature-flags-3lm1
[7] https://github.com/laravel/pennant
[8] https://stackoverflow.com/questions/77078816/how-to-disable-caching-for-laravel-pennant-in-db
[9] https://filamentphp.com/plugins/maartenpaauw-pennant