Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

raobilal4822's avatar

call afterCreate method in relationmanagers

I want to call afterCreate() function in filament relation manager like this

class UnitsRelationManager extends RelationManager
{
    protected static string $relationship = 'units';

    protected static ?string $recordTitleAttribute = 'reference';

    public function isReadOnly(): bool
    {
        return false;
    }

    protected function afterCreate(Model $record): void
    {
        dd('here');
    } ```
but this is not calling
0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! In Filament's RelationManager, the afterCreate() method is not called automatically after a record is created. Instead, you should override the afterCreate() hook on the form returned by the form() method.

Here's how you can do it:

use Filament\Forms;

class UnitsRelationManager extends RelationManager
{
    protected static string $relationship = 'units';

    protected static ?string $recordTitleAttribute = 'reference';

    public function isReadOnly(): bool
    {
        return false;
    }

    public static function form(Forms\Form $form): Forms\Form
    {
        return $form
            ->schema([
                // ... your form fields here
            ])
            ->afterCreate(function (Model $record) {
                dd('here');
            });
    }
}

Key Points:

  • The afterCreate() hook should be chained on the form instance, not as a protected method of the class.
  • The closure receives the newly created $record.

References:

Let me know if you need a more detailed example!

Please or to participate in this conversation.