This is the official documentation of the forestadmin/laravel-forestadmin v2+ and forestadmin/symfony-forestadmin PHP agents.
Forest Admin allows creating new Fields on any Collection, either computationally, by fetching data on an external API or based on other data that is available on the connected data sources.
By default, the fields that you create will be read-only, but you can make them filterable, sortable, and writable by using the relevant methods.
How does it work?
When creating a new field you will need to provide:
List of fields that you need from the source records and linked records in order to run the handler
getValues
Handler which computes the new value for a batch of records
enumValues (optional)
When columnType is Enum, you must specify the values that the field will support
Examples
Adding a field by concatenating other fields
This example adds a user.displayName field, which is computed by concatenating the first and last names.
useForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;useForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Computed\ComputedDefinition;useForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\ConditionTree\Operators;// "User" Collection has the following structure: { id, firstName, lastName }$forestAgent->customizeCollection('User',function (CollectionCustomizer $builder) { $builder->addField('displayName',newComputedDefinition(// Type of the new field columnType: 'String',// Dependencies which are needed to compute the new field (must not be empty) dependencies: ['firstName','lastName'],// Compute function for the new field// Note that the function computes the new values in batches: the return value must be// an array which contains the new values in the same order than the provided records. values: fn ($records) => collect($records)->map(fn ($record) => $record['firstName'] .' '. $record['lastName']), )); });
Adding a field that depends on another computed field
This example adds a user.displayName field, which is computed by concatenating the first and last names, and then another which capitalize it.
useForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;useForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Computed\ComputedDefinition;useForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\ConditionTree\Operators;// "User" Collection has the following structure: { id, firstName, lastName }$forestAgent->customizeCollection('User',function (CollectionCustomizer $builder) {// Create a first field which is computed by concatenating the first and last names $builder->addField('displayName',newComputedDefinition( columnType: 'String', dependencies: ['firstName','lastName'], values: fn ($records) => collect($records)->map(fn ($record) => $record['firstName'] .' '. $record['lastName']), ));// Create a second field which is computed by uppercasing the first field->addField('displayNameCaps',newComputedDefinition( columnType: 'String', dependencies: ['displayName'],// It is legal to depend on another computed field values: fn ($records) => collect($records)->map(fn ($record) => strtoupper($record['displayName'])), )) });
Adding a field that depends on a many-to-one relationship
We can improve the previous example by adding the city of the user to the display name.
useForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;useForestAdmin\AgentPHP\DatasourceCustomizer\Decorators\Computed\ComputedDefinition;useForestAdmin\AgentPHP\DatasourceToolkit\Components\Query\ConditionTree\Operators;// Structure:// User { id, addressId, firstName, lastName }// Address { id, city }$forestAgent->customizeCollection('User',function (CollectionCustomizer $builder) { $builder->addField('displayName',newComputedDefinition( columnType: 'String',// We added 'address:city' in the list of dependencies,// which tells forest to fetch the related record dependencies: ['firstName','lastName','address:city'], values: fn ($records) => collect($records)->map(fn ($record) => $record['firstName'] .' '. $record['lastName'] .' (from '. $record['address']['city'] .' )'), )); });
Adding a field that depends on a one-to-many relationship
Let's now add a user.totalSpending field by summing the amount of all orders.