Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

troccoli's avatar

Laravel Pennant - how to control a feature for all users

I've just started using Pennant so I mostly likely am missing something.

My requirements is that features are controlled in one place for all users. They are not active or inactive depending on the user, they are either active or inactive, for all users.

My initial thought was to user the array store, and then the default for a feature will actually make a query to the DB (probably not using the Pennant's features table to avoid confusion).

I think that would work, but is there a better solution? A more Pennant one perhaps?

0 likes
4 replies
LaryAI's avatar
Level 58

Certainly! If you want to control a feature globally (active/inactive for all users) using Laravel Pennant, the recommended approach is to use the Database Store and set the feature's value globally (without scoping it to a user).

Here’s how you can do it:

1. Use the Database Store

By default, Pennant's database store supports global features. When you set a feature without specifying a scope (like a user), it applies to everyone.

2. Setting a Feature Globally

You can enable or disable a feature for all users like this:

use Laravel\Pennant\Feature;

// Enable the feature for everyone
Feature::activate('your-feature-name');

// Disable the feature for everyone
Feature::deactivate('your-feature-name');

This will store the feature as globally active/inactive in the features table.

3. Checking the Feature

When you check the feature, just do:

if (Feature::active('your-feature-name')) {
    // Feature is enabled for everyone
}

No need to pass a user or any scope.

4. No Need for Custom Array Store

You do not need to use the array store or make custom DB queries. Pennant's built-in database store already supports global features out of the box.

5. Managing Features

You can manage these features via artisan commands as well:

php artisan pennant:activate your-feature-name
php artisan pennant:deactivate your-feature-name

Summary:
Just use Feature::activate() and Feature::deactivate() without a scope. Pennant will handle the global state for you, no custom logic required!

troccoli's avatar
troccoli
OP
Best Answer
Level 6

Although @laryai answers is not correct (the default scope is the user not a global scope), it nevertheless pointed me to the right direction.

All I need to to is set the default scope as the global one by adding the following the the boot() method in the AppServiceProvider

Feature::resolveScopeUsing(fn ($driver) => null);

And I can now use Pennant as it's intended to be used.

martinbean's avatar

@troccoli I don’t really understand what you were trying to achieve? If you just want to enable/disable a feature for all users, then you can do so by just returning a boolean value:

Feature::define('feature-one', fn () => true); // Feature one is active for everyone

Feature::define('feature-two', fn () => false); // Feature two is inactive for everyone

You don’t need to change drivers or mess around with default scopes at all.

troccoli's avatar

Thank you @martinbean

I want to be able to toggle the feature on and off for all user, per environment. The idea is that the feature is active on UAT but disabled in production, until we decide to deliver it. But I also want to be able to switch it on/off without code changes.

If I'm not mistaken, doing it like you suggested (although checking the environment) I would also have to clear the cache that Pennant uses, right?

Please or to participate in this conversation.