Manage multiple nodes of the same type in a Graph
mainWhen a Graph contains multiple entities of the same type (e.g., multiple Person nodes), you can use unique identifiers to track and modify them. If no identifier is provided, a reserved keyword default is used. You can add nodes via chaining or by passing a closure to the graph method.
use Spatie\SchemaOrg\Graph;
use Spatie\SchemaOrg\Person;
$graph = new Graph();
// add a Person using chaining with an identifier
$graph->person('freekmurze')
->givenName('Freek')
->familyName('Van der Herten')
->alternateName('freekmurze');
// add a Person using a closure
$graph->person('sebastiandedeyne', function(Person $sebastian, Graph $graph): void {
$sebastian
->givenName('Sebastian')
->familyName('De Deyne')
->alternateName('sebastiandedeyne');
});
// Update an existing node using its identifier
$graph->person('gummibeer')
->givenName('Tom')
->familyName('Witkowski');
// Hide a specific node by identifier
$graph->hide(Person::class, 'random');
echo json_encode($graph);