In your logic, the return will depend on the order of which the rights are returned. Eg. manage_entries is the first in the rights array, it will return $query. If view_any_entries is the first in the rights array, it will return $query->where('user_id', $user->id);
What you, I assume, want to do is check all rights for manage_entries and return unconstrained query, otherwise constrain on individual rights.
public static function indexQuery(NovaRequest $request, $query)
{
$user = $request->user();
$roles = $user->roles;
$canManageEntries = false;
$canViewEntries = false;
foreach ($roles as $role) {
$rights = $role->rights;
foreach ($rights as $right) {
if ($right->name == 'manage_entries') {
$canManageEntries = true;
}
if ($right->name == 'view_any_entries') {
$canViewEntries = true;
}
}
}
if ($canManageEntries) { // Return unconstrained query when `manage_entries` found
return $query;
} elseif ($canViewEntries) { // Return constrained query when `view_any_entries` found
return $query->where('user_id', $user->id);
}
// Return null if neither rights found.
return null;
}