Custom Table Action

Hi team I am adding a custom table action that can active and de-active a community this is my code but it seems not to work as expected Action::make("de-activate") ->action( fn (Community $record) => $record->update([ 'is_active' => true ]) )
->color('danger')
20 Replies
toeknee
toeknee8mo ago
is_active fillable on the modal.
codeartisan
codeartisan8mo ago
Yeah it is protected $fillable = [ 'name', 'description', 'is_active', 'account_name', 'account_number', 'account_balance', 'leader_id' ]; And also how do I make that action dynamic as well
ashar
ashar8mo ago
what it shows ?? do it show some sort of error on anything ?
einnlleinhatt_
einnlleinhatt_8mo ago
My bad you need to active and deactivate
ashar
ashar8mo ago
no in that case it would be de_Active = true i gues
codeartisan
codeartisan8mo ago
no the field is called is_active Yeah it works but I need to make it dynamic and also show a success message
einnlleinhatt_
einnlleinhatt_8mo ago
Action::make("Buttong")
->action(
function (Community $record) {
$record->update([
'is_active' => !$record->is_active
]);
}
)
->color(function (Community $record) {
return $record->is_active ? 'danger' : 'success';
});
Action::make("Buttong")
->action(
function (Community $record) {
$record->update([
'is_active' => !$record->is_active
]);
}
)
->color(function (Community $record) {
return $record->is_active ? 'danger' : 'success';
});
codeartisan
codeartisan8mo ago
How best can I do that
ashar
ashar8mo ago
how are you expecting it to work ?? @codeartisan didnt get what you actually want :/ is it working , not wotking or working but not upto your expectations ? be very clear with what you asking for 🙂
codeartisan
codeartisan8mo ago
As per now it works the value is actually being changed I just need to make the button name dynamic and show a success message for either actiavted or de-activated thank you very much everyone
toeknee
toeknee8mo ago
You can also trigger a Filamentphp Notification
ChesterS
ChesterS8mo ago
Can you try this and see if it works?
Action::make("deActivate")
->action(
function (Community $record) {
$record->is_active = false;
$record->save();
}
)
Action::make("deActivate")
->action(
function (Community $record) {
$record->is_active = false;
$record->save();
}
)
codeartisan
codeartisan8mo ago
works fine as well
ChesterS
ChesterS8mo ago
Oh shit nvm didn't see that the original works as well, my bad.
codeartisan
codeartisan8mo ago
Guys am left with making the button name dynamic Action::make("Buttong") ->action( function (Community $record) { $record->update([ 'is_active' => !$record->is_active ]); Notification::make() ->title('Community status updated') ->success() ->send(); } ) ->color(function (Community $record) { return $record->is_active ? 'danger' : 'success'; }) anyone with an idea about making the button name dynamic
einnlleinhatt_
einnlleinhatt_8mo ago
Action::make(function (Community $record) {
return $record->is_active ? 'Deactivate' : 'Activate';
})
->action(function (Community $record) {
$record->update([
'is_active' => !$record->is_active
]);
Notification::make()
->title('Community status updated')
->success()
->send();
})
->color(function (Community $record) {
return $record->is_active ? 'danger' : 'success';
});
Action::make(function (Community $record) {
return $record->is_active ? 'Deactivate' : 'Activate';
})
->action(function (Community $record) {
$record->update([
'is_active' => !$record->is_active
]);
Notification::make()
->title('Community status updated')
->success()
->send();
})
->color(function (Community $record) {
return $record->is_active ? 'danger' : 'success';
});
Something like this?
codeartisan
codeartisan8mo ago
my bad actually anything in the table can take in a function thank you guys For some reason that does not work the make function expects a string not a function
einnlleinhatt_
einnlleinhatt_8mo ago
Ohh maybe you can warp it inside label()?
codeartisan
codeartisan8mo ago
incase anyone needs the same functionality it works fine Action::make("Activation") ->action( function (Community $record) { $record->update([ 'is_active' => !$record->is_active ]); Notification::make() ->title('Community status updated') ->success() ->send(); } ) ->color(function (Community $record) { return $record->is_active ? 'danger' : 'success'; }) ->label(function (Community $record) { return $record->is_active ? 'Deactivate' : 'Activate'; }), thank you very much everyone
3rgo
3rgo8mo ago
Why didn't you use the ToggleColumn ? https://filamentphp.com/docs/3.x/tables/columns/toggle You can use the lifecycle hook to display the notification