performing / laravel-field-attributes
This is my package laravel-field-attributes
1.0
2025-05-24 09:41 UTC
Requires
- php: ^8.4
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
README
Simple package to decorate classes with field attributes, allowing you to easily convert data objects to forms with labels and descriptions.
Installation
You can install the package via composer:
composer require performing/laravel-field-attributes
Usage
class MyDto { use FieldAttributes; public function __construct( #[Field(label: 'Name', description: 'Enter your full name')] public string $name, #[Field(label: 'Email', description: 'Enter your email address')] public string $email, ) {} } $instance = new MyDto('John Doe', 'john@example.com'); $instance->getFieldValues(); // ['name' => 'John Doe', 'email' => 'john@example.com'] $instance->getFieldDefinitions(); // [ // 'name' => ['label' => 'Name', 'description' => 'Enter your full name', 'type' => 'string', 'required' => true, 'value' => 'John Doe'], // 'email' => ['label' => 'Email', 'description' => 'Enter your email address', 'type' => 'string', 'required' => true, 'value' => 'john@example.com'] // ]
If you combine this with spatie/laravel-data, you can also add validation on top, serialize and deserialize as JSON back and forth from the database.
In the package there is also a caster ObjectCast
which can cast a json column to an object, it takes the class name from a type
column of the eloquent model. You can also map types to specifc classes in the config.
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.