Tommika79
Tommika79
FFilament
Created by Tommika79 on 8/26/2024 in #❓┊help
Filament Multi-Select Not Loading Default Values from Third Table (Not Pivot Table)
Hi all, I'm facing an issue with Filament in Laravel where a multi-select field isn't loading its default values in the edit form. My setup involves saving selected values in a third table, which isn't a traditional pivot table but rather an additional table (CarUsageRecord) related to a Task model. The Problem When editing a record, the default function in Select::make doesn't run, so the previously selected values from the third table aren't loaded into the select field, making it impossible to see what was selected before. My Setup Task model: Has a one-to-many relationship with CarUsageRecord. CarUsageRecord model: Stores car_id and task_id to track which cars are linked to a task. Task model relationship:
public function carUsageRecords():
{
return $this->hasMany(CarUsageRecord::class);
}
public function carUsageRecords():
{
return $this->hasMany(CarUsageRecord::class);
}
Form setup in the Filament resource:
Select::make('car_id')
->relationship('car', 'license_plate')
->multiple()
->reactive()
->saveRelationshipsUsing(function ($component, $state) {
$record = $component->getRecord();
CarUsageRecord::where('task_id', $record->id)->delete();
foreach ($state as $carId) {
CarUsageRecord::create([
'car_id' => $carId,
'task_id' => $record->id,
]);
}
})
Select::make('car_id')
->relationship('car', 'license_plate')
->multiple()
->reactive()
->saveRelationshipsUsing(function ($component, $state) {
$record = $component->getRecord();
CarUsageRecord::where('task_id', $record->id)->delete();
foreach ($state as $carId) {
CarUsageRecord::create([
'car_id' => $carId,
'task_id' => $record->id,
]);
}
})
What I've Tried Attempted to use the default method to load selected car IDs, but it doesn’t run during form load. Used dump() and Log::info() for debugging inside the default method, but no output indicates the method is not executing. Saving works fine, but loading the values during edit is the issue. What I Need How can I ensure the default method runs during form load? What’s the best way to load values into a multi-select field from a third table? Any advice would be appreciated!
4 replies
FFilament
Created by Tommika79 on 8/3/2024 in #❓┊help
Visual Inconsistencies in Filament Repeater with Dynamic Select Options
We are using a Filament form with a Repeater that contains a Select field to choose workers from the User model, filtered by the Employee role. The requirement is that once a worker is selected in one row, they should not appear in the Select options of other rows.
Select::make('worker_id')->reactive()
->options(function (callable $get) {
$allUsers = User::role('Employee')->pluck('name', 'id');
$selectedUsers = collect($get('../../taskGroups'))->pluck('worker_id')->filter()->toArray();

return $allUsers->filter(function ($name, $id) use ($selectedUsers) {
return !in_array($id, $selectedUsers);
});
})
->rules([
function ($component) {
return function (string $attribute, $value, Closure $fail) use ($component) {
$items = $component->getContainer()->getParentComponent()->getState();
$selected = array_column($items, $component->getName());

if (count(array_unique($selected)) < count($selected)) {
$fail('A worker can only be selected once.');
}
};
},
])
Select::make('worker_id')->reactive()
->options(function (callable $get) {
$allUsers = User::role('Employee')->pluck('name', 'id');
$selectedUsers = collect($get('../../taskGroups'))->pluck('worker_id')->filter()->toArray();

return $allUsers->filter(function ($name, $id) use ($selectedUsers) {
return !in_array($id, $selectedUsers);
});
})
->rules([
function ($component) {
return function (string $attribute, $value, Closure $fail) use ($component) {
$items = $component->getContainer()->getParentComponent()->getState();
$selected = array_column($items, $component->getName());

if (count(array_unique($selected)) < count($selected)) {
$fail('A worker can only be selected once.');
}
};
},
])
Issue: When selecting a worker, the selected worker's name visually jumps to the next available ID after about half a second. However, the originally selected worker does disappear from the list, indicating that the selection is correctly registered but not correctly displayed. Due to the character limit, I will share another thing I tried in the comments Question: Am I doing something wrong, or is this a bug in the Filament framework? Any assistance or suggestions on how to handle this scenario correctly would be greatly appreciated.
4 replies
FFilament
Created by Tommika79 on 7/28/2024 in #❓┊help
Help Needed with DateTimePicker in Repeater
No description
3 replies