ToggleColumn

Hello everyone, I have currently tried to ToggleColumn with a relation to the user (so that each user can activate / deactivate certain scripts) The relation is correct so far, unfortunately I had to add the value "used" in the main table "delete_reason". Although I have the link of “delete_reason” and ‘user’ in the table “delete_reason_users”. According to the tutorial and ChatGPT I have already tried a custom view, unfortunately I did not manage to send the action “checkbox” from the blade view with the livewire component (although I created it). Is there somehow a workarount without having the “used” in the table, because the link is in the relation table.
3 Replies
Devsome
DevsomeOP2w ago
->columns([
Tables\Columns\ToggleColumn::make('used')
->label(__('deletions.table.use'))
->getStateUsing(function ($record) {
return $record->user()->where('user_id', auth()->id())->exists();
})
->afterStateUpdated(function ($state, $record) {
$userId = auth()->id();

if ($state) {
$record->user()->syncWithoutDetaching([$userId]);
$body = 'deletions.notification.success_added';
} else {
$record->user()->detach($userId);
$body = 'deletions.notification.success_removed';
}
Notification::make()
->title(__('deletions.notification.success_title'))
->body(__($body))
->success()
->send();
}),
Tables\Columns\TextColumn::make('reason')
->label(__('deletions.table.reason'))
->searchable(),
Tables\Columns\TextColumn::make('state')
->label(__('deletions.table.state'))
->formatStateUsing(fn($state) => Str::ucfirst($state))
->searchable(),
])
->columns([
Tables\Columns\ToggleColumn::make('used')
->label(__('deletions.table.use'))
->getStateUsing(function ($record) {
return $record->user()->where('user_id', auth()->id())->exists();
})
->afterStateUpdated(function ($state, $record) {
$userId = auth()->id();

if ($state) {
$record->user()->syncWithoutDetaching([$userId]);
$body = 'deletions.notification.success_added';
} else {
$record->user()->detach($userId);
$body = 'deletions.notification.success_removed';
}
Notification::make()
->title(__('deletions.notification.success_title'))
->body(__($body))
->success()
->send();
}),
Tables\Columns\TextColumn::make('reason')
->label(__('deletions.table.reason'))
->searchable(),
Tables\Columns\TextColumn::make('state')
->label(__('deletions.table.state'))
->formatStateUsing(fn($state) => Str::ucfirst($state))
->searchable(),
])
Devsome
DevsomeOP2w ago
after putting "used" into the delete_reason table it works.
No description
No description
toeknee
toeknee5d ago
If you have the relationship setup then why not just use dot notation?

Did you find this page helpful?