Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I handle feature flags in Laravel Pennant while using Laravel Cashier


How do I handle feature flags in Laravel Pennant while using Laravel Cashier


To handle feature flags in Laravel Pennant while using Laravel Cashier, you can integrate feature flag logic that depends on the user's subscription status managed by Cashier. Here's a detailed approach based on Laravel Pennant's capabilities and how it can work with Cashier's subscription checks:

Defining Feature Flags with Laravel Pennant and Laravel Cashier

1. Install and Configure Laravel Pennant
First, ensure Laravel Pennant is installed and configured in your Laravel application. Feature flags are typically defined in a service provider, such as a dedicated `FeatureServiceProvider`, to keep the logic organized.

2. Create Feature Flags Based on Subscription Status
Laravel Cashier manages subscriptions and provides methods like `hasActivePlan()` or `subscribed()` on the authenticated user model to check if a user has an active subscription. You can leverage these methods inside your feature flag definitions.

3. Example Feature Flag Definition
In your `FeatureServiceProvider` or wherever you define feature flags, use the `Feature::define` method to create a flag that activates a feature only for subscribed users:

php
   use Laravel\Pennant\Feature;
   use App\Models\User;

   Feature::define('premium-feature', function (User $user): bool {
       // Use Laravel Cashier's subscription check
       return $user->subscribed('default'); // 'default' is the subscription name
   });
   

This closure receives the authenticated `User` instance and returns `true` if the user has an active subscription, enabling the feature for subscribed users only[3][6].

4. Using the Feature Flag in Your Application
To conditionally show or enable features based on this flag, use the `Feature::active()` method in your controllers, views, or middleware:

php
   use Laravel\Pennant\Feature;

   if (Feature::active('premium-feature')) {
       // Show or enable premium feature
   } else {
       // Fallback for non-subscribed users
   }
   

This method automatically passes the authenticated user to the closure defined in the feature flag, so the subscription check is performed seamlessly[3][4].

Additional Tips

- Complex Conditions: You can extend the feature flag logic to include more complex conditions, such as checking subscription tiers, trial periods, or user roles alongside subscription status.

- Rich-Value Flags: Laravel Pennant supports returning rich values from feature flags. For example, you could return different feature variants based on subscription plan type for A/B testing or tiered feature access[3].

- Centralized Management: Defining feature flags in a service provider centralizes control, making it easy to update feature rollout logic without scattering checks throughout the codebase[4].

- Integration with UI: If you use admin panels like Filament, there are packages that integrate Pennant feature flags into UI for easier management and segmentation, which can complement your subscription-based feature gating[5][7].

Summary

To handle feature flags in Laravel Pennant when using Laravel Cashier, define feature flags that check the authenticated user's subscription status via Cashier's methods within the feature flag closure. Then, gate features throughout your app by checking these flags with `Feature::active()`. This approach provides a clean, maintainable way to progressively roll out subscription-based features.

This method is supported and recommended by Laravel Pennant's design and examples, making it straightforward to integrate with Laravel Cashier's subscription management[3][6].

Citations:
[1] https://laravel.com/docs/11.x/pennant
[2] https://laravel-news.com/feature-flags-with-laravel-pennant
[3] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[4] https://dev.to/saurabh-dhariwal/how-to-manage-feature-flags-with-laravel-pennant-in-2024-1phb
[5] https://github.com/stephenjude/filament-feature-flags
[6] https://apiacademy.treblle.com/laravel-api-course/feature-flags
[7] https://filamentphp.com/plugins/maartenpaauw-pennant
[8] https://www.reddit.com/r/laravel/comments/10yto7q/laravel_pennant_simple_and_lightweight_feature/
[9] https://dev.to/bennett/basic-feature-flags-in-laravel-1bld
[10] https://github.com/codinglabsau/laravel-feature-flags