Enable or Disable Toggle by default when first page load

Hi Everyone i have problem when i create custom function schema on my grid to show list based on checklists that i have Here's the code, and i got trouble when i want to set by default the toggle is enable/active/on is everyone have any good approach to solve this problem ?
Grid::make('checklists')
->columns(2)
->schema(function ($record): array {
$checklists = JobChecklist::where('job_report_checklist_id', $record->id)->get();
$components = [];
foreach ($checklists as $checklist) {

$components[] = Toggle::make("checklist_{$checklist->id}_check")
->label('Completed')
->inline(false)
->default(function () use ($checklist) {
return $checklist->check;
})
->reactive()
->afterStateUpdated(function ($state, callable $get) use ($checklist) {
$jobChecklist = JobChecklist::find($checklist->id);
$jobChecklist->update(['check' => $state]);
});
}

return $components;
})
->columnSpanFull(),
Grid::make('checklists')
->columns(2)
->schema(function ($record): array {
$checklists = JobChecklist::where('job_report_checklist_id', $record->id)->get();
$components = [];
foreach ($checklists as $checklist) {

$components[] = Toggle::make("checklist_{$checklist->id}_check")
->label('Completed')
->inline(false)
->default(function () use ($checklist) {
return $checklist->check;
})
->reactive()
->afterStateUpdated(function ($state, callable $get) use ($checklist) {
$jobChecklist = JobChecklist::find($checklist->id);
$jobChecklist->update(['check' => $state]);
});
}

return $components;
})
->columnSpanFull(),
No description
33 Replies
CodeWithDennis
CodeWithDennis2mo ago
This image is of the create form? @Ramdani Syaputra
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
Edited form, and inside repeater
CodeWithDennis
CodeWithDennis2mo ago
Yea so on the edit form there is no "default" because it checks for record details. Is there a reason why not just use a relationship repeater?
Repeater::make('yourRepeaterName')
->relationship('checklists')
->schema([
Toggle::make('check'),
])
Repeater::make('yourRepeaterName')
->relationship('checklists')
->schema([
Toggle::make('check'),
])
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
i did but the result is empty thats why im using custom schema in that grid is there any good approach for this ?
CodeWithDennis
CodeWithDennis2mo ago
Ah, i think i understand what you want to do. Wouldn't a checkbox list field be better here tho?
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
Yes its better, but still same, the result are empty if im using relationship, also when im using custom schema its also cannot "checked" by the result of $checklist->check
CodeWithDennis
CodeWithDennis2mo ago
Thats probably because you haven;'t setup your relations the proper way. To attach "checklists" to a model, first establish the appropriate relationships. Once that's done, creating a relational field becomes straightforward. Assuming you have a table named checklists with the columns; id, name, created_at, updated_at, you can create a pivot table to connect this table with your model (or use a morph relationship). This approach allows you to use a checkbox list to easily add or remove checklists from your model, eliminating the need to toggle a button.
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
I did already so basically i have WorkJob model and it has relation to JobReportChecklist model and the relation is HasOne
public function jobReportChecklist()
{
return $this->hasOne(JobReportChecklist::class);
}
public function jobReportChecklist()
{
return $this->hasOne(JobReportChecklist::class);
}
and also inside JobReportChecklist i have relation to JobChecklist with hasMany relation
public function checklists()
{
return $this->hasMany(JobChecklist::class);
}
public function checklists()
{
return $this->hasMany(JobChecklist::class);
}
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
And also here my schema repeater the result in grid its just showing the default input and the default toggle not my data in database from checklists relation
No description
CodeWithDennis
CodeWithDennis2mo ago
So this model you are currently editing has relational results? Your $model->jobReportChecklist returns anything ?
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
Yep this part
No description
CodeWithDennis
CodeWithDennis2mo ago
I’m sorry, but I’m not quite sure what you’re trying to achieve. 😅 When you set up a relationship with a repeater it should fill the repeater with the records from the relationship (which seems to be what is happening) what is exactly the problem?
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
I want just to make the button is set to active/enable when first time access edit page, thats it, also its kinda hard to do i already did what you mention before but the result for this checklists its still empty
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
i did what you mention before, but this is for another section, only checklists section is weird for me
No description
CodeWithDennis
CodeWithDennis2mo ago
But you are editing a record which has the value on false?
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
on true actually
No description
CodeWithDennis
CodeWithDennis2mo ago
Oke so when you click on edit it says false?
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
yes, i want the toggle follow the check on my database, when its "1" then on
CodeWithDennis
CodeWithDennis2mo ago
It should do that already when you edit. You sure the column name matches?
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
when i hit the toggle to "on" it did updated on the table, but when i reload, its become "off" again but the "check" field its still "on(1)"
CodeWithDennis
CodeWithDennis2mo ago
Yea so its something with your column or relationship.
Repeater::make('jobReportCheckList')
->relationship()
->schema([
TextInput::make('name'),
Toggle::make('check'),
])
Repeater::make('jobReportCheckList')
->relationship()
->schema([
TextInput::make('name'),
Toggle::make('check'),
])
Try this and see if it works (Just for testing)
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
actually the checklists its inside jobReportChecklist so the parent is jobReportChecklist its showing this data
CodeWithDennis
CodeWithDennis2mo ago
? I think you might want to reconsider your approach. As I mentioned, with the right relationship, it can be much easier and should work seamlessly right from the start.
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
Yes i got your advice, maybe i need to see which part im doing it wrong, and thanks for your help 😄
CodeWithDennis
CodeWithDennis2mo ago
Isn't this what you are trying to do?
CodeWithDennis
CodeWithDennis2mo ago
@Ramdani Syaputra
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
Yes correct i did on this part and its success
CodeWithDennis
CodeWithDennis2mo ago
So it works?
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
see this maybe you have idea
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
thats the problem i currently face in last second video or is there any approach that we can "force" the toggle to become true/false ?
CodeWithDennis
CodeWithDennis2mo ago
Again i think its your relational setup.
Ramdani Syaputra
Ramdani SyaputraOP2mo ago
Okay thanks ill check back my code
Want results from more Discord servers?
Add your server