This is my package laravel-field-attributes
performing/laravel-field-attributes is a Laravel package for this is my package laravel-field-attributes.
It currently has 0 GitHub stars and 1.769 downloads on Packagist (latest version 1.1.0).
Install it with composer require performing/laravel-field-attributes.
Discover more Laravel packages by performing
or browse all Laravel packages to compare alternatives.
Last updated
Simple package to decorate classes with field attributes, allowing you to easily convert data objects to forms with labels and descriptions.
You can install the package via composer:
composer require performing/laravel-field-attributes
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', '[email protected]');
$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' => '[email protected]']
// ]
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.
composer test
The MIT License (MIT). Please see License File for more information.