A Laravel package to manage settings for any Eloquent model. This package allows you to attach settings to any model using the HasSettings trait, retrieve settings with default values, and delete settings.
-
Install the package via Composer:
composer require grhone/laravel-settings
-
Add the service provider to
config/app.phpif using Laravel version below 5.5:'providers' => [ // Other service providers... Grhone\LaravelSettings\SettingsServiceProvider::class, ],
-
Publish the migration file:
php artisan vendor:publish --provider="Grhone\LaravelSettings\SettingsServiceProvider" --tag="migrations"
-
Run the migrations:
php artisan migrate
To use settings with your Eloquent models, simply add the HasSettings trait to your model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Grhone\LaravelSettings\Traits\HasSettings;
class Event extends Model
{
use HasSettings;
public function getWebsiteBannerFilename()
{
// Get the setting with a default value
return $this->getSetting('websiteBannerFilename', 'default_banner.jpg');
}
}Retrieve the setting value for a given key. If the setting does not exist, the default value is returned.
$banner = $event->getSetting('websiteBannerFilename'); // without default value
$banner = $event->getSetting('websiteBannerFilename', 'default_banner.jpg'); // with default valueSet the setting value for a given key.
$event->setSetting('websiteBannerFilename', 'banner123.jpg');Delete the setting for a given key.
$event->deleteSetting('websiteBannerFilename');Thank you for considering contributing to the Laravel Settings Package! You can contribute in many ways, including:
- Reporting a bug
- Discussing the current state of the code
- Submitting a fix
- Proposing new features
The Laravel Settings Package is open-source software licensed under the MIT license.