F
Filament14mo ago
Matthew

Action that changes value in Form

When the action Accept is pressed, the dropdown in the form should change to "Yes"
class EditExpense extends EditRecord
{
protected static string $resource = ExpenseResource::class;

protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
Action::make('accept')
->color('success')
->label('Accept')
->disabled(!auth()->user()->can('status_expense'))
->requiresConfirmation()
// ->action(function (Status $record, array $data): void {
// $record->author()->where('name', 'Yes')->first()->id;
// $record->save();
// })
// ->mountUsing(fn (Forms\ComponentContainer $form, User $record) => $form->fill([
// 'status' => Status::where('name', 'Yes')->first()->id,
// ]))
,
// Action::make('reject')
// ->color('danger')
// ->label('Reject')
// ->action('rejectAction')
// ->disabled(!auth()->user()->can('status_expense')),
];
}

}
class EditExpense extends EditRecord
{
protected static string $resource = ExpenseResource::class;

protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
Action::make('accept')
->color('success')
->label('Accept')
->disabled(!auth()->user()->can('status_expense'))
->requiresConfirmation()
// ->action(function (Status $record, array $data): void {
// $record->author()->where('name', 'Yes')->first()->id;
// $record->save();
// })
// ->mountUsing(fn (Forms\ComponentContainer $form, User $record) => $form->fill([
// 'status' => Status::where('name', 'Yes')->first()->id,
// ]))
,
// Action::make('reject')
// ->color('danger')
// ->label('Reject')
// ->action('rejectAction')
// ->disabled(!auth()->user()->can('status_expense')),
];
}

}
12 Replies
Matthew
Matthew14mo ago
So far, when I click accept nothing happens If i use this
->action(function (Status $record, array $data): void {
$record->author()->where('name', 'Yes')->first()->id;
$record->save();
})
->action(function (Status $record, array $data): void {
$record->author()->where('name', 'Yes')->first()->id;
$record->save();
})
Then I get this error App\Filament\Resources\ExpenseResource\Pages\EditExpense::App\Filament\Resources\ExpenseResource\Pages{closure}(): Argument #1 ($record) must be of type App\Models\Status, null given,
Dan Harrin
Dan Harrin14mo ago
you need to get the status from $data $record doesnt exist yet
Matthew
Matthew14mo ago
But, im on the edit page. Doesnt that mean that a record exist?
Dan Harrin
Dan Harrin14mo ago
you can use $this->record
Matthew
Matthew14mo ago
protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
Action::make('accept')
->color('success')
->label('Accept')
->disabled(!auth()->user()->can('status_expense'))
->requiresConfirmation()
->action(function (Status $record, array $data): void {
$this->record->where('name', 'Yes')->first()->id;
$record->save();
}),
];
}
protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
Action::make('accept')
->color('success')
->label('Accept')
->disabled(!auth()->user()->can('status_expense'))
->requiresConfirmation()
->action(function (Status $record, array $data): void {
$this->record->where('name', 'Yes')->first()->id;
$record->save();
}),
];
}
like this?
Dan Harrin
Dan Harrin14mo ago
->action(function (array $data): void {
$this->record->where('name', 'Yes')->first()->id;
$this->record->save();
})
->action(function (array $data): void {
$this->record->where('name', 'Yes')->first()->id;
$this->record->save();
})
Matthew
Matthew14mo ago
Its still giving an error
Matthew
Matthew14mo ago
using phpmyadmin/tinker I can verify that the "Yes", exists
> use App\Models\Status
> Status::all()->pluck('name','id');
= Illuminate\Support\Collection {#8132
all: [
2 => "No",
3 => "Pending",
4 => "Yes",
],
}
> use App\Models\Status
> Status::all()->pluck('name','id');
= Illuminate\Support\Collection {#8132
all: [
2 => "No",
3 => "Pending",
4 => "Yes",
],
}
Dan Harrin
Dan Harrin14mo ago
please read my code carefully you did not copy it
Matthew
Matthew14mo ago
you're right. I still had the previous copy Could you please explain why we use $data in this case and not $record?
Dan Harrin
Dan Harrin14mo ago
we only pass $record to the action in some places. mainly table actions. we should probably pass it in all places, but it will always be identical to $this->record on edit/view pages
Matthew
Matthew14mo ago
But now, how do we know that we are changing the value of a Select in the form? figured it out:)
->action(function (array $data): void {
$this->record->status = Status::where('name', 'Accepted')->first()->id;
$this->record->save();
}),
->action(function (array $data): void {
$this->record->status = Status::where('name', 'Accepted')->first()->id;
$this->record->save();
}),
Sorry for asking so many questions Dan but I dont have a lot of experience with Laravel/Filament or PHP