The primary way to check flags is through the Feature facade:
use FeatureFlags\Facades\Feature;
The most common use case - check if a feature is enabled:
// Simple check
if (Feature::enabled('new-checkout')) {
return view('checkout.new');
}
return view('checkout.legacy');
// Or use the inverse
if (Feature::disabled('maintenance-mode')) {
// App is not in maintenance
}
Target specific users by providing context:
// With a user model
if (Feature::for($user)->enabled('beta-features')) {
// This specific user has beta access
}
// With custom context array
$context = [
'id' => 'user_123',
'email' => 'user@example.com',
'plan' => 'pro',
'country' => 'GB',
];
if (Feature::for($context)->enabled('uk-only-feature')) {
// User matches the targeting rules
}
With auto_resolve enabled (default), the package automatically uses the authenticated user:
// These are equivalent when a user is logged in:
Feature::enabled('premium-feature');
Feature::for(auth()->user())->enabled('premium-feature');
Flags aren't limited to boolean values. Get string, number, or JSON values:
// String flag
$theme = Feature::value('theme-variant'); // 'dark', 'light', 'auto'
// Number flag
$limit = Feature::value('api-rate-limit'); // 100, 500, 1000
// JSON flag
$config = Feature::value('onboarding-config');
// Returns: ['steps' => 3, 'showVideo' => true, ...]
Provide fallback values for when a flag doesn't exist or isn't enabled:
// Boolean with default
$enabled = Feature::enabled('unknown-flag', false);
// Value with default
$limit = Feature::value('rate-limit', 100);
// Complex default
$config = Feature::value('feature-config', [
'enabled' => false,
'variant' => 'control',
]);
Use convenient Blade directives in your views:
@feature('new-dashboard')
<x-new-dashboard />