packy
4 days ago

Settings global helper

Posted 4 days ago by packy

I am following the guide here: https://laravel-news.com/global-application-settings but keep getting the error Target [App\Settings] is not instantiable. Not sure what is causing this. I have got it to work using directly in a controller like $settings = Valuestore::make(storage_path('app/settings.json')); but I need a global solution.

Settings.php

<?php

namespace App;

use Spatie\Valuestore\Valuestore;

class Settings extends Valuestore
{
    //
}

helpers.php

<?php



function settings($key = null, $default = null)
{
    if ($key === null) {
        return app(App\Settings::class);
    }

    return app(App\Settings::class)->get($key, $default);
}

in AppServiceProvider


 public function register()
    {
        $this->app->singleton(Settings::class, function () {
            return Settings::make(storage_path('app/settings.json'));
        });
    }

in View

{{ settings('facebook_url') }}

Please sign in or create an account to participate in this conversation.