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

phayes0289's avatar

Accessing Global Settings in Spatie Laravel-Settings

I am working with the Spatie laravel-settings package. On every blade page I have a block of text at the top of the page that I ONLY want it to display if the client has turned on a variable called “page_help” for all the users of the app. “page_help” is a boolean setting. I do not understand how I can I make “page_help” available globally.

This is my settings area “laravel-settings” config page:

<?php

return [

    /*
     * Each settings class used in your application must be registered, you can
     * put them (manually) here.
     */
    'settings' => [
            GeneralSettings::class
    ],
]

This is my “GeneralSettings” class page in \app\settings

<?php

namespace App\Settings;

use Spatie\LaravelSettings\Settings;

class GeneralSettings extends Settings
{
    public string $site_name;
    
    public bool $site_active;

    public bool $page_help;

    public static function group(): string
    {
        return 'GeneralSettings';
    }
}

This is my general settings migration that I have run, inserting the geberal settings values in the database:

<?php

use Spatie\LaravelSettings\Migrations\SettingsMigration;

class CreateGeneralSettings extends SettingsMigration
{
    public function up(): void
    {
        $this->migrator->add('general.site_name', 'FireOps');
        $this->migrator->add('general.site_active', true);
        $this->migrator->add('general.page_help', true);
    }
}

This is the simple block of static code I want to make visible if “turned on”. something similiar resides at the top of just about each and every blade page.

{{-- Make this block of code visible if page_help is set to true --}}
    <small>
        This is the "Posts Index." Here you can search for and read and post that has not expired. Expired posts will not show up in the search results.
        You can search the title and body.
    </small>
{{-- End block of code --}}

How should I be using that global variable to accomplish this?

0 likes
4 replies
LaryAI's avatar
Level 58

To make the page_help setting globally available in all your Blade views, you can share the setting with all views using a view composer or a service provider. Here's how you can do it using a service provider:

  1. Open the AppServiceProvider or create a new service provider (e.g., SettingsServiceProvider).

  2. In the boot method of your service provider, use the view facade to share the page_help setting with all views.

Here's an example using the AppServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use App\Settings\GeneralSettings;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Share the 'page_help' setting with all views
        View::composer('*', function ($view) {
            $settings = app(GeneralSettings::class);
            $view->with('page_help', $settings->page_help);
        });
    }

    // ...
}

After adding this code to your service provider, the page_help variable will be available in all your Blade templates. You can then use it to conditionally display the block of text like this:

@if($page_help)
    <small>
        This is the "Posts Index." Here you can search for and read and post that has not been expired. Expired posts will not show up in the search results.
        You can search the title and body.
    </small>
@endif

Make sure that your service provider is registered in the config/app.php file under the providers array if you are creating a new one.

This way, you can easily control the visibility of the help text block across all your views by updating the page_help setting in your GeneralSettings.

rapa's avatar
@if(app(\App\Settings\GeneralSettings::class)->page_help)

{{-- Make this block of code visible if page_help is set to true --}}
    <small>
        This is the "Posts Index." Here you can search for and read and post that has not expired. Expired posts will not show up in the search results.
        You can search the title and body.
    </small>
{{-- End block of code --}}

@endif
1 like
Artwork's avatar

@rapa indeed, as in the readme, they utilize the package custom container automated resolution:

function getName(): string{
    return app(GeneralSettings::class)->site_name;
}

Source

Please or to participate in this conversation.