PHP code example of mrdth / laravel-model-settings

1. Go to this page and download the library: Download mrdth/laravel-model-settings library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mrdth / laravel-model-settings example snippets


return [
    'column' => env('MRDTH_MODEL_SETTINGS_COLUMN_NAME', 'settings'),
];

...
    $table->json('settings')->nullable();
...

...
use Illuminate\Notifications\Notifiable;
use Mrdth\LaravelModelSettings\Concerns\HasSettings;

class User extends Authenticatable
{
    use HasFactory, Notifiable, HasSettings;
...

$user = User::find(1);
$user->hasSetting('use custom avatar'); // false

$user->setSetting('use custom avatar', true);
$user->hasSetting('use custom avatar'); // true
$user->getSetting('use custom avatar'); // true

$user->updateSetting('use custom avatar', false);
$user->getSetting('use custom avatar'); // false
$user->getSetting('non-existent setting'); // null
$user->getSetting('non-existent setting', 'default value'); // 'default value'

$user->getSettings(); // ['use custom avatar' => false]

$user->deleteSetting('use custom avatar');
$user->hasSetting('use custom avatar'); // false

$user->deleteSettings();

$users = User::whereSetting('use custom avatar')->get(); // Returns all users with the setting 'use custom avatar'
$users = User::whereSetting('use custom avatar', true)->get(); // Returns all users with the setting 'use custom avatar' set to true
bash
php artisan vendor:publish --tag="laravel-model-settings-config"
bash
php artisan make::msm {model}