Check if value is true, and check the checkbox on update

I have a multi tenancy setup. But when i want to edit an tenancy I want to have all checkboxes checked if they are true in the json object from the database. How can I do this?
CheckboxList::make('settings')
->options([
'twitter' => 'Twitter',
'linkedin' => 'LinkedIn',
'whatsapp' => 'WhatsApp',
'email' => 'Email',
'copy' => 'Copy',
])
->descriptions([
'twitter' => 'Enable sharing on Twitter (x)',
'linkedin' => 'Enable sharing on LinkedIn',
'whatsapp' => 'Enable sharing on WhatsApp',
'email' => 'Enable sharing on Email',
'copy' => 'Enable sharing through coping the link',
])
CheckboxList::make('settings')
->options([
'twitter' => 'Twitter',
'linkedin' => 'LinkedIn',
'whatsapp' => 'WhatsApp',
'email' => 'Email',
'copy' => 'Copy',
])
->descriptions([
'twitter' => 'Enable sharing on Twitter (x)',
'linkedin' => 'Enable sharing on LinkedIn',
'whatsapp' => 'Enable sharing on WhatsApp',
'email' => 'Enable sharing on Email',
'copy' => 'Enable sharing through coping the link',
])
{
"copy": false,
"email": false,
"twitter": true,
"linkedin": false,
"whatsapp": false
}
{
"copy": false,
"email": false,
"twitter": true,
"linkedin": false,
"whatsapp": false
}
So if in this example twitter is true, then I want to have the checkbox for twitter checked. Is this possible?
4 Replies
awcodes
awcodes2y ago
Is settings a relationship to the model or a db column on the model.?
alex123x5
alex123x5OP2y ago
It is a db column I've already tried it with using ->default([false, false, true, false, false]) but that doesn't work
awcodes
awcodes2y ago
i would create a record through the filament form to see exactly how the data is stored in the db, then use that to reconstruct it where you need to. Also keep in mind that ->default() only works in the 'create' context. So you can't use it to prefil the field in the 'edit' context.
alex123x5
alex123x5OP2y ago
I've created this one (from the example) through the form. Or I don't understand it that is also possible 😉 Didn't know what happened but now, with a fresh database it works. So yeah either way, thanks for the suggestions!

Did you find this page helpful?