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.