Fields

This is the official documentation of the forestadmin/laravel-forestadmin v2+ and forestadmin/symfony-forestadmin PHP agents.

Forest Admin is a platform to administrate your business operations efficiently; it provides powerful features to ease data navigation and implement high level views.

When designing databases and APIs, the way to go is usually to push for normalization. This means ensuring there is no redundancy of data (all data is stored in only one place), and that data dependencies are logical.

On the other hand, graphical user interfaces usually need duplication and shortcuts to be user-friendly.

To bridge that gap, Forest Admin allows adding, moving, removing, and overriding behaviors from fields.

Minimal example

use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Computed\ComputedDefinition;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\ConditionTree\Operators;

$forestAgent->customizeCollection(
    'User',
    function (CollectionCustomizer $builder) {
        $builder->addField(
            'fullName',
            new ComputedDefinition(
                columnType: 'String',
                dependencies: ['firstName', 'lastName'],
                values: fn ($records) => collect($records)->map(fn ($record) => $record['firstName'] . ' ' . $record['lastName']),
            )
        )
            // Make it writable
            ->replaceFieldWriting(
                'fullName',
                function($value) {
                    [$firstName, $lastName] = explode(' ', $value);

                    return compact('firstName', 'lastName');
                }
            )

            // Add validators
            ->addFieldValidation('fullName', Operators::PRESENT)
            ->addFieldValidation('fullName', Operators::SHORTER_THAN, 30)
            ->addFieldValidation('fullName', Operators::GREATER_THAN, 2)

           // Make it filterable and sortable
            ->emulateFieldFiltering('fullName')
            ->emulateFieldSorting('fullName')

            // Remove previous fields
            ->removeField('firstName', 'lastName');
        }
    );

Last updated