Settings

Settings

Store and retrieve settings generally or for model objects in Laravel.

Moamen Eltouny (Raggi)
PHP Version : >= 7.2 Laravel Version : >= 6.0 License Support
Source Packagist Version Packagist Downloads

General Settings Settingable Models

Install

Install the latest version using Composer

composer require pharaonic/laravel-settings
php artisan migrate

OptionalPublish the migrations files.

php artisan vendor:publish --tag=laravel-settings
General Settings

Using settings helper to store and retrieve the general settings.

SET

settings()->status = false;                     // Way 1

settings()->set('off_message', 'Maintenance');  // Way 2

settings()->set([                               // Way 3
    'status'        => true,
    'off_message'   => 'Maintenance'
]);

// Save all settings
settings()->save();

GET

settings()->status;                             // Way 1
// Returns string|null

settings()->get('off_message');                 // Way 2
// Returns string|null

$data = settings()->get(['status', 'off_message']);     // Way 3
// Returns Collection

echo $data->off_message->value;
Settingable Models

For using settings with models directly.

Just add this trait Settingable to your auth model.

namespace App\Models;

...
use Pharaonic\Laravel\Settings\Traits\Settingable;

class User extends Authenticatable
{
    use Settingable;

    ...
}

SET

$user->settings->multilingual = true;           // Way 1

$user->settings->set('notifiable', true);       // Way 2

$user->settings->set([                          // Way 3
    'multilingual'  => true,
    'notifiable'    => true,
]);

// Save all settings
$user->settings->save();

GET

$user->settings->notifiable;                                    // Way 1
// Returns string|null

$user->settings->get('multilingual');                           // Way 2
// Returns string|null

$data = $user->settings->get(['multilingual', 'notifiable']);   // Way 3
// Returns Collection

echo $data->multilingual->value;