androlax2 / laravel-model-typed-settings
A Laravel package that adds type-safe settings attributes to Eloquent models with automatic casting and validation
Fund package maintenance!
Androlax2
Installs: 218
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/androlax2/laravel-model-typed-settings
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
README
Stop treating your model settings like messy associative arrays. This package allows you to cast JSON columns directly into Type-Safe DTOs with support for nested objects, enums, and automatic type coercion.
Installation
You can install the package via composer:
composer require androlax2/laravel-model-typed-settings
Usage
Define your Settings Class
Create a class that extends Androlax2\LaravelModelTypedSettings\Settings. Use standard PHP properties with types and default values.
<?php namespace App\Settings; use Androlax2\LaravelModelTypedSettings\Settings; use Androlax2\LaravelModelTypedSettings\Attributes\AsCollection; class UserPreferences extends Settings { public string $theme = 'light'; public int $items_per_page = 15; public bool $notifications_enabled = true; // Support for Backed Enums public UserStatus $status = UserStatus::Active; // Support for Collections of Enums #[AsCollection(NotificationChannel::class)] public array $channels = [NotificationChannel::Email]; // Support for Nested Settings public SecuritySettings $security; }
Apply to Eloquent Model
Add the settings class to your model's $casts array.
<?php use App\Models\User; use App\Settings\UserPreferences; class User extends Model { protected $casts = [ 'preferences' => UserPreferences::class, ]; }
Add to database the column for preferences :
<?php Schema::create('users', function (Blueprint $table) { $table->settingColumn('preferences'); });
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.