Certainly! In Filament (assuming you are using Filament since you mentioned "RelationManager" and "Resource"), the getRelations() method on a Resource is a static method. This means you do not have direct access to the current record instance as you would in instance methods (like in a RelationManager or Form/Detail pages where you get a $record parameter).
Why can't you get the record directly in getRelations()?
getRelations()is called to define which relation managers are available for the resource in general, not for a specific record.- At the time
getRelations()is called, there is no guarantee that a specific record context is available.
What are your options?
1. Use request()->route('record') (as you are doing)
This is the most common workaround, but as you noticed, it's not as clean as having the record injected.
2. Use RelationManager's canView() or canAccess() methods
If you need to conditionally show/hide relation managers based on the record, you can always return all possible relation managers in getRelations(), and then use the canView() or canAccess() methods on the RelationManager to control visibility based on the current $record.
Example:
// In your Resource
public static function getRelations(): array
{
return [
UnitRelationManager::class,
ActivitiesTimelineRelationManager::class,
WorkOrdersRelationManager::class,
WorkOrdersFeedbackRelationManager::class,
TenanciesRelationManager::class,
DocumentRelationManager::class,
ListingsRelationManager::class,
];
}
// In your RelationManager
public static function canViewForRecord(Model $ownerRecord): bool
{
// Example: Only show for Staff
return $ownerRecord->type === UserTypeEnum::Staff->value;
}
This way, you don't need to hack around with the request. Filament will call canViewForRecord() for each relation manager, passing the current record.
3. Use a custom page or override Filament's behavior (advanced)
If you really need to customize which relation managers are available per record, you would need to override Filament's resource page logic, which is not recommended unless you are comfortable with the internals.
Summary / Recommendation
- Best Practice: Return all possible relation managers in
getRelations(), and usecanViewForRecord()in each RelationManager to conditionally show/hide them based on the current record. - Why: This is the cleanest and most "Filament way" to handle this, and avoids relying on the request or session.
Let me know if you need a more concrete example for your specific user types!