Using existing Resource for the relation manager of another resource

Can I reuse a complete resource for the relation manager within another resource? So I don't have to define everything again?
5 Replies
Johan
JohanOP2y ago
Nevermind found this: https://github.com/filamentphp/demo/blob/main/app/Filament/Resources/Shop/CategoryResource/RelationManagers/ProductsRelationManager.php If I have that same relation anywhere else I can better put the Relation in a more general folder I guess? In my case I have Riders which can have users. I have a rider resource and user resource, I managed to re-use the user form and table in the relation manager, by using the example above. But the user resource has some custom creation stuff like (in the App\Filament\Resources\UserResource\Pages\CreateUser page)
protected function afterCreate(): void
{
/** @var User $user */
$user = $this->record;

$user->notify(new NewAccountNotification());
}
protected function afterCreate(): void
{
/** @var User $user */
$user = $this->record;

$user->notify(new NewAccountNotification());
}
I was expecting this to be triggered in the relation manager also, but its not? The notification could also be triggered on the created event of an user of course but I dont want users which register on the frontend to receive this e-mail.
Dan Harrin
Dan Harrin2y ago
check the relation manager docs. we have ->after() methods for doing that there when you call a form and table from a relation manager, thats the only thing that gets changed. the page-specific customisations are independent
Johan
JohanOP2y ago
Ah ok! So for now the best is to duplicate the logic? To the after method too
wyChoong
wyChoong2y ago
It’s php, you can decouple/refactor it anyways you want, and call it wherever you need
Johan
JohanOP2y ago
Yea of course I can create an Action or something its more like I have to remember to call it on both places
Want results from more Discord servers?
Add your server