This is the official documentation of the forestadmin/laravel-forestadmin v2+ and forestadmin/symfony-forestadmin PHP agents.
Relationships that point to multiple records are displayed in the frontend in the "Related Data" and "Explorer" Tab.
One-to-Many relations
In a one-to-many relationship, one record from a Collection is attached to multiple records of another Collection.
Think about countries and towns: a country has multiple towns, and each town belongs to a country.
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\SymfonyForestAdmin\Service\ForestAgent;
// Link 'Country' to 'Town'
$forestAgent->customizeCollection(
'Country',
function (CollectionCustomizer $builder) {
$builder->addOneToManyRelation(
name: 'myTowns',
foreignCollection: 'Town',
originKey: 'country_id',
originKeyTarget: 'id' // Optional (uses `Country` primary key by default)
);
}
);
Many-to-Many relations
In a many-to-many relation, 3 Collections are used instead of 2 to build the relation.
This allows multiple records from one Collection to be attached to multiple records from another Collection.
For instance, on a movie recommendation website, each user can rate many movies, and each movie can be rated by many users. The 3 Collections that are used are users (the "origin" Collection), ratings (the "through" Collection), and movies (the "foreign" Collection).
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\SymfonyForestAdmin\Service\ForestAgent;
// Create one side of the relation ...
$forestAgent->customizeCollection(
'User',
function (CollectionCustomizer $builder) {
$builder->addManyToManyRelation(
name: 'ratedMovies',
foreignCollection: 'Movie',
throughCollection: 'Rating'
originKey: 'user_id',
originKeyTarget: 'id', // Optional (uses primary key of User by default)
foreignKey: 'movie_id',
foreignKeyTarget: 'id' // Optional (uses primary key of Movie by default)
);
}
)
// ... and the other one.
->customizeCollection(
'Movie',
function (CollectionCustomizer $builder) {
// ⚠️ Not 'OneToOne'
$builder->addManyToOneRelation(
name: 'whoRatedThisMovie',
foreignCollection: 'User',
throughCollection: 'Rating'
originKey: 'movie_id',
originKeyTarget: 'id', // Optional (uses primary key of Movie by default)
foreignKey: 'user_id',
foreignKeyTarget: 'id' // Optional (uses primary key of User by default)
);
}
);
External relations
External relations allow to define Collections which will only be available through the "Related Data" section or a given model.
Note that external relations do not support pagination.
<?php
use ForestAdmin\AgentPHP\Agent\Utils\Env;
use ForestAdmin\AgentPHP\DatasourceCustomizer\CollectionCustomizer;
use ForestAdmin\AgentPHP\DatasourceDoctrine\DoctrineDatasource;
use ForestAdmin\SymfonyForestAdmin\Service\ForestAgent;
return static function (ForestAgent $forestAgent) {
$forestAgent->agent->addDatasource(
new DoctrineDatasource($forestAgent->getEntityManager(), [
'url' => Env::get('DATABASE_URL'),
])
)->customizeCollection('address', function (CollectionCustomizer $builder) {
$states = [
[ 'code' => 'AK', 'name' => 'Alaska', 'zipMin' => 99501, 'zipMax' => 99950, 'closeTo' => [] ],
[ 'code' => 'AL', 'name' => 'Alabama', 'zipMin' => 35004, 'zipMax' => 36925, 'closeTo' => ['TE', 'MI', 'GE', 'FL'] ],
[ 'code' => 'AR', 'name' => 'Arkansas', 'zipMin' => 71601, 'zipMax' => 72959, 'closeTo' => ['OK', 'TX', 'LO', 'MI'] ],
[ 'code' => 'AZ', 'name' => 'Arizona', 'zipMin' => 85001, 'zipMax' => 86556, 'closeTo' => ['NM', 'CO', 'UT', 'NE'] ],
[ 'code' => 'CA', 'name' => 'California', 'zipMin' => 90001, 'zipMax' => 96162, 'closeTo' => ['OR', 'NE', 'AZ'] ],
// ....
];
$builder->addExternalRelation(
'nearStates',
[
// Define schema of the records in the relation.
'schema' => ['code' => 'String', 'name' => 'String'],
// Which fields are needed from the parent record to run the handler?
// Dependencies are optional: by default only the primary key of address would be provided
'dependencies' => ['country', 'zipCode'],
'listRecords' => function ($record) use ($states) {
if ($record['country'] === 'USA') {
$state = array_filter($states, fn ($s) => $s['zipMin'] < $record['zipCode'] && $record['zipCode'] < $s['zipMax']);
$state = array_shift($state);
return array_filter($states, fn ($s) => (in_array($s['code'], $state['closeTo'])));
}
return [];
},
]
);
}
);
};