@php
// Organize nodes by their IDs for easy reference
$nodes = collect($family_node['nodes'])->keyBy('id');
// Build adjacency list for relationships
$adjacency = [];
foreach ($family_node['edges'] as $edge) {
$from = $edge['from'];
$to = $edge['to'];
if (!isset($adjacency[$from])) {
$adjacency[$from] = [];
}
$adjacency[$from][] = $to;
}
/**
* Recursive function to render nodes and their children
*/
function renderNode($nodeId, $nodes, $adjacency) {
$node = $nodes->get($nodeId);
if (!$node) return '';
// Start rendering node
$html = '
';
$html .= '
';
$image = $node['user_image_link'] ?: asset('admin/images/'.$node['label'].'.png');
$html .= '
';
$html .= '
';
$html .= '
' . htmlspecialchars($node['label']) . '
';
$html .= '
';
// Render children recursively if any
if (isset($adjacency[$nodeId])) {
$children = $adjacency[$nodeId];
$html .= '
';
foreach ($children as $childId) {
$html .= '
';
$html .= renderNode($childId, $nodes, $adjacency);
$html .= '
';
}
$html .= '
';
}
return $html;
}
// Find the root node (labeled as "Father")
$rootNode = collect($family_node['nodes'])->firstWhere('label', 'Father');
// If no "Father" is found, check for "Mother"
if (!$rootNode) {
$rootNode = collect($family_node['nodes'])->firstWhere('label', 'Mother');
}
// If no "Mother" is found, check for "You"
if (!$rootNode) {
$rootNode = collect($family_node['nodes'])->firstWhere('label', 'You');
}
@endphp
@if ($rootNode)
{{-- Render root node --}}
{{-- Render family members connected to the root --}}
@php
$children = $adjacency[$rootNode['id']] ?? [];
@endphp
@foreach ($children as $childId)
{!! renderNode($childId, $nodes, $adjacency) !!}
@endforeach
@else
No family members found.
@endif