Home » Spatie Laravel Settings – How to Use Guide

Spatie Laravel Settings – How to Use Guide

Loved it? Please Share on Social Media

INTRODUCTION:

Hello, fellow developers. What’s up?, I hope you are doing fine. In this extended tutorial, we will learn how to install the spatie laravel settings package.

Spatie Laravel Settings Package INSTALLATION:

At first, we will install our package using the below composer command. copy & paste the following into your terminal and hit enter from your keyboard.

composer require spatie/laravel-settings

Tip: You can publish the migrations file with the below command.

php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations"
php artisan migrate

Also, we can publish the config file using the below command.

php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="config"

Creating Setting using Spatie Laravel Settings Package:

You can create your custom setting as per your requirements by using the below command. Just copy & paste the below command into your terminal and hit enter.

php artisan make:setting GeneralSetting --group=general

Then, you have to modify that setting file as per your needs. You can get a clear idea from the below code.

use Spatie\LaravelSettings\Settings;

class GeneralSettings extends Settings

{
    public string $site_name;

    public bool $site_active;

    public static function group(): string

    {
        return 'general';
    }
}

I’ve added two fields which are site_name site_active, you can add more fields later.

Registering Setting to Config File:

Next, you have to register this setting into the package config file which we created earlier. You will get that config file in the config folderNow, enter your setting name into that setting key just like below. 

/*
 * Each settings class used in your application must be registered, you can
 * add them (manually) here.
*/

    'settings' => [
        GeneralSettings::class
    ],

Creating Setting Migration File using Spatie Laravel Settings:

Now, in this stage, we will create a migration file for our general setting.

For that, you just have to copy & paste the following command into your terminal & hit enter.

php artisan make:settings-migration CreateGeneralSettings

Our migration file creation is complete then, you have to modify that migration file as per your needs. You can get a clear idea from the below code.

use Spatie\LaravelSettings\Migrations\SettingsMigration;
class CreateGeneralSettings extends SettingsMigration

{
    public function up(): void
    {
        $this->migrator->add('general.site_name', 'Spatie');
        $this->migrator->add('general.site_active', true);
    }
}

Read Also: Laravel Disable Auto Discovery – 6 Simplified Ways To Do That

MIGRATE THE SETTING MIGRATION FILE:

Now, we will migrate our general migration file using the artisan command. Please copy & paste the below command into your terminal & hit enter and you are done!

php artisan migrate

USING SETTINGS USING CONTROLLER:

Now, in this stage we will learn how we can use our global settings from our laravel controller file. please follow the below controller code or you can modify the below code as per your demand.

class IndexController

{
    public function __invoke(GeneralSettings $settings){
        return view('index', [
            'site_name' => $settings->site_name,
        ]);
    }
}

TEST THE APPLICATION:

At last, we will start our local server using our artisan command. php artisan serve If there is no problem, our code will work fine, and the project will be running now at http://127.0.0.1:8000/ or http://locahost:8000/. You can learn more from spatie laravel settings documentation.

CONCLUSION:

Finished! That’s all about our tutorial. I’ve tried to teach you the most updated & tested things in this tutorial. Thank you so much for reading the whole tutorial from the beginning. If this tutorial helps you a little bit, then remember to share this post on social media. If you have questions, suggestions, or tips regarding this post, let us know via our Contact Us page.


Loved it? Please Share on Social Media

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.