@php $selectedParents = []; $primaryIndex = 0; if (count(old('parent_user_ids', [])) > 0) { foreach (old('parent_user_ids') as $i => $pid) { if (!$pid) continue; $selectedParents[] = [ 'index' => $i, 'id' => $pid, 'name' => old('parent_names')[$i] ?? 'Orang Tua', 'relation' => old('parent_relations')[$i] ?? 'WALI', 'is_primary' => old('primary_parent') !== null && (int) old('primary_parent') === $i, ]; if (old('primary_parent') !== null && (int) old('primary_parent') === $i) { $primaryIndex = $i; } } } elseif (isset($student) && $student->exists) { $selectedParents = $student->parents()->orderBy('pivot_is_primary', 'desc')->get()->map(function ($p, $i) { return [ 'index' => $i, 'id' => $p->id, 'name' => $p->name, 'relation' => $p->pivot->relation, 'is_primary' => (bool) $p->pivot->is_primary, ]; })->values()->toArray(); $primaryIndex = collect($selectedParents)->firstWhere('is_primary')['index'] ?? 0; } @endphp
Orang Tua / Wali (opsional, bisa lebih dari satu)
@foreach ($selectedParents as $sp) @include('admin.student._parent-selected', [ 'idx' => $sp['index'], 'id' => $sp['id'], 'name' => $sp['name'], 'relation' => $sp['relation'], 'isPrimary' => $sp['is_primary'], 'primaryIndex' => $primaryIndex, ]) @endforeach
@error('parent_user_ids')

{{ $message }}

@enderror
@push('scripts') @endpush