To a single record

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

Relationships that point to a single record are displayed in the GUI as links.

Once configured, they can be used in charts ↗, filters ↗, scopes ↗, and segments ↗.

Note that, for performance reasons when sorting a Table View on customizer-defined relations, Forest Admin will always sort on the primary key of the related collection.

Many-to-One relations

Many-to-One relations are by far the most common type of relation: many records from a Collection are attached to another Collection record.

Think about countries and towns: a town belongs to a single country, but each country can have multiple towns.

use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\SymfonyForestAdmin\Service\ForestAgent;

$forestAgent->customizeCollection(
    'Town',
    function (CollectionCustomizer $builder) {
        $builder->addManyToOneRelation(
            name: 'country',
            foreignCollection: 'Country',
            foreignKey: 'country_id',
            foreignKeyTarget: 'id' // Optional (uses `Country` primary key by default)
        );
    }
);

One-to-One relations

In a one-to-one relation, there is a one-to-one mapping between records in 2 Collections. The relation can be unset for some records, but no record from the first Collection can be linked to more than one record in the other Collection.

Think about cities and mayors: A city can have at most one mayor, and each mayor belongs to a single city.

Take note that the inverse of a one-to-one is a many-to-one.

This may seem counter-intuitive: the side of the relation which should be configured as many-to-one is the one that carries the foreign key.

use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\SymfonyForestAdmin\Service\ForestAgent;

// Configure one side of the relation ...
$forestAgent->customizeCollection(
    'Mayor',
    function (CollectionCustomizer $builder) {
        $builder->addOneToOneRelation(
            name: 'city',
            foreignCollection: 'City',
            originKey: 'mayor_id',
            originKeyTarget: 'id' // Optional (uses `Mayor` primary key by default)
        );
    }
)
    // ... and the other one.
    ->customizeCollection(
        'City',
        function (CollectionCustomizer $builder) {
            // ⚠️ Not 'OneToOne'
            $builder->addManyToOneRelation(
                name: 'mayor',
                foreignCollection: 'Mayor',
                foreignKey: 'mayor_id',
                foreignKeyTarget: 'id' // Optional (uses `Mayor` primary key by default)
            );
        }
    );

Last updated