F
Filament17mo ago
miomech

Filament v2 Is it possible to render a relation manager outside of the admin dashboard on blade

Hello I was hoping someone could guide me in the right direction, I'm pretty familiar with the v2 docs but cant seem to find anything about how to render a relation manager in a regular blade template. There are plenty of docs for how to use tables and forms in blade but nothing about how to use relationmanagers alongside them tried using phind.com (AI) to see if it would throw out some random ideas and it suggested this
PHP
$postsRelationManager = new PostsRelationManager($parentModel, $relationship);
$postsRelationManager->render();
PHP
$postsRelationManager = new PostsRelationManager($parentModel, $relationship);
$postsRelationManager->render();
but of course that does not seem to work
3 Replies
Mark Chaney
Mark Chaney17mo ago
No. You just would create a normal table and set your actions to accomplish the same thing An RM is specific to a resource and the panel builder It’s just a premade table and set of actions, you can definitely create identical functionality with a standalone table
miomech
miomechOP17mo ago
Thank you for your help @mark_chaney
Passenger
Passenger17mo ago
Yes, it is possible. You'd need to mount the relation managers on your component.
protected function getRelationManagers(): array
{
$managers = $this->getResource()::getRelations();

return array_filter(
$managers,
function (string | RelationGroup $manager): bool {
if ($manager instanceof RelationGroup) {
return (bool) count($manager->getManagers(ownerRecord: $this->getOwnerRecord()));
}

return $manager::canViewForRecord($this->getOwnerRecord());
},
);
}

public function loadRelationManagers(): void
{
$managers = $this->getRelationManagers();

if (array_key_exists($this->activeRelationManager, $managers)) {
return;
}
$this->relationManagers = $managers;
$this->activeRelationManager = array_key_first($this->relationManagers) ?? null;
}

public function getOwnerRecord()
{
//return the $parentModel.
// return $this->ownerRecord = $this->getTableQuery()->find($this->recordId);
}
protected function getRelationManagers(): array
{
$managers = $this->getResource()::getRelations();

return array_filter(
$managers,
function (string | RelationGroup $manager): bool {
if ($manager instanceof RelationGroup) {
return (bool) count($manager->getManagers(ownerRecord: $this->getOwnerRecord()));
}

return $manager::canViewForRecord($this->getOwnerRecord());
},
);
}

public function loadRelationManagers(): void
{
$managers = $this->getRelationManagers();

if (array_key_exists($this->activeRelationManager, $managers)) {
return;
}
$this->relationManagers = $managers;
$this->activeRelationManager = array_key_first($this->relationManagers) ?? null;
}

public function getOwnerRecord()
{
//return the $parentModel.
// return $this->ownerRecord = $this->getTableQuery()->find($this->recordId);
}
Then in your blade file you can render something like this
@if (count($relationManagers))
<x-filament::hr />

<x-filament::resources.relation-managers
:active-manager="$activeRelationManager"
:managers="$relationManagers"
:owner-record="$ownerRecord"
:page-class="static::class"
>
</x-filament::resources.relation-managers>
@endif
@if (count($relationManagers))
<x-filament::hr />

<x-filament::resources.relation-managers
:active-manager="$activeRelationManager"
:managers="$relationManagers"
:owner-record="$ownerRecord"
:page-class="static::class"
>
</x-filament::resources.relation-managers>
@endif
Make sure to call $this->loadRelationManagers() when your component pounts/page loads.
Want results from more Discord servers?
Add your server