News

Laravel Model Settings

Published
Laravel Model Settings image

Laravel Model Settings is a package by Lorand Gombos to provide model settings configuration in a database field or a separate database table.

The package ships with two traits—settings in a field or table—that you can configure in a given model (one or the other, not both). First, the settings field is a JSON field:

// Settings field
 
use Glorand\Model\Settings\Traits\HasSettingsField;
 
class User extends Model
{
use HasSettingsField;
 
//define only if you select a dirrerent name from the default
public $settingsFieldName = 'user_settings';
 
}

And a separate settings table option:

use Glorand\Model\Settings\Traits\HasSettingsTable;
 
class User extends Model
{
use HasSettingsTable;
}

Here’s some basic usage examples in a model:

// Get all settings
$user->settings()->all();
 
// Get a specific setting
$user->settings()->get('some.setting');
$user->settings()->get('some.setting', 'default value');
 
// Add/Update
$user->settings()->apply((array)$settings);
$user->settings()->set('some.setting', 'new value');
$user->settings()->update('some.setting', 'new value');
 
// Remove
$user->settings()->delete('some.setting');

You can learn more about this package, get full installation instructions, and view the source code on GitHub at glorand/laravel-model-settings.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

tinkerwell logo
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article