Laravel Pennant is fully capable of handling different feature flag scenarios for trial periods with detailed, flexible control over feature activation based on user attributes, roles, or custom logic. Here's how it supports such scenarios:
Defining Feature Flags with Conditional Logic
You can define feature flags in Laravel Pennant using closures that receive the authenticated user as a parameter, allowing you to implement complex logic such as trial period checks. For example, you could enable a feature only for users currently in a trial period by checking a user attribute or trial expiration date:
php
use Laravel\Pennant\Feature;
use App\Models\User;
Feature::define('trial-feature', function (User $user) {
return $user->trial_ends_at && $user->trial_ends_at->isFuture();
});
This closure returns `true` if the user's trial period is still active, enabling the feature only during that time[2][4][6].
Activating and Deactivating Features Dynamically
Pennant allows activating or deactivating features on a per-user basis or globally. For example, after a user upgrades from trial to paid subscription, you can activate the feature for them:
php
Feature::for($user)->activate('trial-feature');
Or deactivate it when the trial ends or subscription is downgraded:
php
Feature::for($user)->deactivate('trial-feature');
You can also activate or deactivate features for everyone at once, useful for rolling out or halting a feature globally[2][4].
Gradual Rollouts and A/B Testing
Pennant supports advanced use cases such as gradual rollouts, which can be useful during trial periods to enable features for a percentage of users:
```phpFeature::define('trial-feature', function ($user) {
return mt_rand(1, 100) ennant-handle-dif-iJljQZ4_SxqtXY3dHT47lA?utm_source=copy_output