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

adiputra22's avatar

Filament get attached model using `after` method

Hi there,

I create model relation manager and I want to get model attached after user attach any model.

public function table(Table $table): Table
    {
        $attachAction = Tables\Actions\AttachAction::make()
            ->preloadRecordSelect()
            ->before(function () {
                $outlet = $this->getTable();
                Log::info('before test get product outlet', [
                    'outlet' => $outlet,
                ]);
            })
            ->after(function () {
                $product = $this->getOwnerRecord();
                $outlet = $this->getViewData();

                Log::info('after test get product outlet', [
                    'product' => $product->toArray(),
                    'outlet' => $outlet,
                ]);
            })
            ->recordSelectOptionsQuery(fn (Builder $query) => $query->where('company_id', auth()->user()->current_company_id));

        return $table
            ->recordTitleAttribute('name')
            ->columns([
                Tables\Columns\TextColumn::make('name'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                $attachAction,
            ])
            ->actions([
                Tables\Actions\DetachAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DetachBulkAction::make(),
                ]),
            ]);
    }

I using method after, but I not sure how to get the attached model/item.

0 likes
3 replies
LaryAI's avatar
Level 58

To get the attached model after an attach action in Filament, you can use the $this->getMountedTableActionRecord() method within the after callback. This method will return the model instance that was just attached.

Here's how you can modify your after method to log the attached model:

public function table(Table $table): Table
{
    $attachAction = Tables\Actions\AttachAction::make()
        ->preloadRecordSelect()
        ->after(function () {
            $product = $this->getOwnerRecord();
            $attachedOutlet = $this->getMountedTableActionRecord();

            Log::info('after test get product outlet', [
                'product' => $product->toArray(),
                'attachedOutlet' => $attachedOutlet->toArray(),
            ]);
        })
        ->recordSelectOptionsQuery(fn (Builder $query) => $query->where('company_id', auth()->user()->current_company_id));

    return $table
        ->recordTitleAttribute('name')
        ->columns([
            Tables\Columns\TextColumn::make('name'),
        ])
        ->filters([
            //
        ])
        ->headerActions([
            $attachAction,
        ])
        ->actions([
            Tables\Actions\DetachAction::make(),
        ])
        ->bulkActions([
            Tables\Actions\BulkActionGroup::make([
                Tables\Actions\DetachBulkAction::make(),
            ]),
        ]);
}

In this example, $this->getMountedTableActionRecord() is used to retrieve the model that was just attached to the owner record. The toArray() method is then called on both the owner record and the attached record to convert them to arrays for logging purposes.

Please note that the actual method names and functionality may vary depending on the version of Filament you are using. Always refer to the official Filament documentation or source code for the most accurate and up-to-date information.

Arthur-Zaripov's avatar

@adiputra22 i had the same problem. In my case related model was extend from Pivot with auto-incrementing primary key . So ID of created model does not return. You should add public $incrementing = true in pivot model.

1 like

Please or to participate in this conversation.