How can I create a ToggleColumn to change status from the list page

I've tried with multiple solution to add a ToggleColumn to list page but didn't get a proper success for this. Issues: Into the database side data is updating for the status column but in the list page toggle is not getting proper record and showing only active/inactive as a toggle. Here is my code, is ther any problem or I'm missing something or else?
ToggleColumn::make('status')
->onColor('success')
->offColor('warning')
->onIcon('heroicon-s-check')
->offIcon('heroicon-s-x-mark')
->label('Status')
->getStateUsing(fn($record): bool => $record->status === 1)
->afterStateUpdated(function ($record, $state, $livewire) {
$record->update(['status' => $state ? 1 : 0]);

Notification::make()
->title('')
->success()
->body('Status updated successfully!')
->send();

$livewire->getTableRecords();
}),
ToggleColumn::make('status')
->onColor('success')
->offColor('warning')
->onIcon('heroicon-s-check')
->offIcon('heroicon-s-x-mark')
->label('Status')
->getStateUsing(fn($record): bool => $record->status === 1)
->afterStateUpdated(function ($record, $state, $livewire) {
$record->update(['status' => $state ? 1 : 0]);

Notification::make()
->title('')
->success()
->body('Status updated successfully!')
->send();

$livewire->getTableRecords();
}),
15 Replies
Vp
Vp2mo ago
Try like this:
ToggleColumn::make('status')
->onColor('success')
->offColor('warning')
->onIcon('heroicon-s-check')
->offIcon('heroicon-s-x-mark')
->label('Status')
// ->getStateUsing(fn($record): bool => $record->status === 1)
->afterStateUpdated(function ($record, $state, $livewire) {
// $record->update(['status' => $state ? 1 : 0]);

Notification::make()
->title('')
->success()
->body('Status updated successfully!')
->send();

// $livewire->getTableRecords();
}),
ToggleColumn::make('status')
->onColor('success')
->offColor('warning')
->onIcon('heroicon-s-check')
->offIcon('heroicon-s-x-mark')
->label('Status')
// ->getStateUsing(fn($record): bool => $record->status === 1)
->afterStateUpdated(function ($record, $state, $livewire) {
// $record->update(['status' => $state ? 1 : 0]);

Notification::make()
->title('')
->success()
->body('Status updated successfully!')
->send();

// $livewire->getTableRecords();
}),
Kuldeep
Kuldeep2mo ago
Yes, this is working but the major issue is that once we change status from active -> inactive. Into database its updating but to the listing page after refreshing it'll display active instead of inactive.
Vp
Vp2mo ago
Can you record some video about what happen.. because I am very confused now..
Kuldeep
Kuldeep2mo ago
Checkout the screenshots. As 2 records are with inactive (0) status but to the list page it still showing active (1) status.
No description
No description
Vp
Vp2mo ago
Is it a boolean column in database? and did you make casts in Model?
Kuldeep
Kuldeep2mo ago
Yes, I've done it already
Vp
Vp2mo ago
what is filament version? it's weird that why it's not working tho
Kuldeep
Kuldeep2mo ago
filament v3 Yeah, same thing also I'm checking that because in the all examples its working properly.
Vp
Vp2mo ago
v3.*.* the exact version.. run php artisan about or composer show filament/filament to view the version
Kuldeep
Kuldeep2mo ago
Laravel: v11.3.1 PHP: v8.2.21 Filament: v3.2.108
Vp
Vp2mo ago
New updates are available on filament and laravel as well.. suggest you to update.. but still I don't think this will resolve this.. And now I am completely out of idea 🤣 Did you change something other than default (creating resource php artisan make:filament-resource Company --generate) except the table column?
Kuldeep
Kuldeep2mo ago
Noop, No any changes.
Vp
Vp2mo ago
can you create reproduction repo.. will check and if it's indeed the bug then we can submit issue on filament github
Kuldeep
Kuldeep2mo ago
You can try from your end as well. Just an update is making a separate enum file with constant to define status active = 1, inactive = 0
Vp
Vp2mo ago
separate enum file with constant to define status active = 1, inactive = 0
You didn't mention this part before.. and you said you did like this.. ToggleColumn is similar as Toggle (Form) which interact a boolean value.
Want results from more Discord servers?
Add your server