FilamentF
Filament2y ago
Pan

Custom Action

What I am trying to do:
I am trying to create a custom action like the AssociateAction however I want to customize it on my own form.

What I did:
I tried following this
https://filamentphp.com/docs/3.x/actions/modals#modal-forms, specifically this part
    ->action(function (array $data, Post $record): void {
        $record->author()->associate($data['authorId']);
        $record->save();
    })

I've changed the $record into $post and the author() into just ->user_id and right now I am testing it ->associate(1) by just passing an int to see if it works.

My issue/the error:
if I kept the $post-user_id()->associate(1) i get the error BadMethodCall
Call to undefined method App\Models\Equipment::user_id()
changing it into post-user_id->associate(1) i get the error Call to a member function associate() on null

Code:
    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('name')
            ->columns([
                Tables\Columns\TextColumn::make('name'),
            ])
            ->headerActions([
                Tables\Actions\Action::make('Borrow Test')
                    ->form([
                        Forms\Components\Select::make('name')
                            ->searchable()
                            ->options(Equipment::query()->pluck('name','barcode'))
                            //->getSearchResultsUsing(fn (string $search): array => Equipment::where('barcode', 'like', "%{$search}%")->limit(50)->pluck('name')->toArray())
                    ])
                    ->action(function (array $data, Equipment $post): void{
                        $post->user_id->associate(1);
                        $post->save();
                    })
          ])

Ignore the other headerActions like AssociateAction and 'Borrow'

Thank you !!!!
Was this page helpful?