Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I update the stored value for a feature flag in Laravel Pennant


How do I update the stored value for a feature flag in Laravel Pennant


To update the stored value for a feature flag in Laravel Pennant, you use the `Feature` facade which provides methods to activate, deactivate, and set rich values for feature flags. Here's a detailed explanation:

Activating and Deactivating Feature Flags

- To activate (enable) a feature flag, call:

php
  Feature::activate('feature-name');
  

This sets the feature flag as active for the default scope (usually the authenticated user).

- To deactivate (disable) a feature flag, call:

php
  Feature::deactivate('feature-name');
  

This sets the feature flag as inactive for the default scope.

- You can also scope the activation or deactivation to a specific user or entity by using the `for()` method:

php
  Feature::for($user)->activate('feature-name');
  Feature::for($user)->deactivate('feature-name');
  

This is useful for enabling or disabling features for particular users or teams.

Setting Rich Values

- Laravel Pennant supports rich feature flag values beyond simple boolean states. To set a specific value for a feature flag, provide the value as the second argument to `activate`:

php
  Feature::activate('purchase-button', 'seafoam-green');
  

This stores `'seafoam-green'` as the active value of the `purchase-button` feature flag.

Forgetting Stored Values

- If you want Pennant to forget the stored value of a feature flag and re-resolve it from the feature definition, use:

php
  Feature::forget('feature-name');
  

Bulk Updates

- You can update feature flags for all users at once:

php
  Feature::activateForEveryone('feature-name', $value = null);
  Feature::deactivateForEveryone('feature-name');
  

For example, to activate a feature with a rich value for everyone:
php
  Feature::activateForEveryone('purchase-button', 'seafoam-green');
  

Summary

- Use `Feature::activate('flag')` or `Feature::deactivate('flag')` to toggle flags on or off.
- Use `Feature::activate('flag', $value)` to set a rich value.
- Use `Feature::for($user)->activate('flag')` to scope changes to a user.
- Use `Feature::forget('flag')` to clear stored values.
- Use `Feature::activateForEveryone('flag', $value)` or `Feature::deactivateForEveryone('flag')` for bulk updates.

This approach ensures consistent storage and retrieval of feature flag values across requests and users, allowing dynamic and flexible feature management in Laravel applications[1][2].

Citations:
[1] https://laravel.com/docs/12.x/pennant
[2] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[3] https://hackernoon.com/how-to-manage-feature-flags-with-laravel-pennant
[4] https://www.reddit.com/r/laravel/comments/10yto7q/laravel_pennant_simple_and_lightweight_feature/
[5] https://bmehul.com/articles/laravel-pennant-a-guide-to-feature-flags
[6] https://dev.to/saurabh-dhariwal/how-to-manage-feature-flags-with-laravel-pennant-in-2024-1phb
[7] https://wpwebinfotech.com/blog/laravel-pennant-guide/
[8] https://elliotderhay.com/blog/how-to-make-laravel-pennant-features-that-apply-globally