@php
// Build owner assignments from old() (priority) or existing user data
$oldOwners = old('owners', []);
$userOwners = isset($user) ? $user->owners->keyBy('id') : collect();
// Convert old() array to collection keyed by id (cast to int for consistent comparison)
$oldOwnersCollection = collect($oldOwners)->keyBy(fn($item) => (int) $item['id']);
@endphp
@foreach($owners as $owner)
@php
// Check if this owner is assigned (from old() first, then user data)
$ownerId = (int) $owner->id;
if ($oldOwnersCollection->has($ownerId)) {
$isAssigned = true;
$role = $oldOwnersCollection->get($ownerId)['role'] ?? 'viewer';
} elseif ($userOwners->has($ownerId)) {
$isAssigned = true;
$role = $userOwners->get($ownerId)->pivot->role;
} else {
$isAssigned = false;
$role = 'viewer';
}
@endphp
@endforeach