Certainly! In Filament (which your code resembles), the getRelations() method on a Resource is static and does not receive the current record instance as an argument. That's why you can't just type-hint Model $record in this static context.
However, if you want to access the current record in a RelationManager or in Resource pages (like Edit, View, etc.), those methods often receive the record instance directly.
Why request()->route('record') is used
In the static getRelations() method, since you don't have access to the record instance, you have to rely on the request or session to fetch the current record ID.
Is there a better way?
Short answer:
For the static getRelations() method, there is no built-in way to get the current record instance directly. You have to use the request or session, as you are doing.
Alternative:
If you want to avoid using request()->route('record'), you could consider overriding the getRelations() method in your Resource Page (like EditUser or ViewUser), where you do have access to the record. But the static getRelations() on the Resource itself will always be static and not have the record.
Example in a Resource Page (Edit/View)
If you are on a page like EditUser, you can do:
public static function getRelations(): array
{
return [
// ... your relations
];
}
// In your EditUser page
public function getRelations(): array
{
$userType = $this->record->type;
return UserResource::getRelationsForUserType($userType);
}
But this only works in the context of a page instance (not the static Resource).
Summary
- In static Resource methods: You must use
request()->route('record')or similar. - In Resource Page classes (Edit, View, etc): You can access
$this->recorddirectly.
Your current approach is correct for the static context.
If you want to avoid this, you must move your logic to a non-static context (like a page or relation manager).
If you have a specific use-case or want to see how to do this in a RelationManager or a Page, let me know!