F
Filamentβ€’13mo ago
Xiquita

Attach Relation Manager

Hello friends, I have a question, how is it that in the attach of a relation manager I attach the same record already registered?
Tables\Actions\AttachAction::make()
->recordSelect(
fn (Select $select) => $select->noSearchResultsMessage(__('equipment.equipment_resource.search_field')),
)
->allowDuplicates()
->color('success')
->preloadRecordSelect()
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('serial_number')
->numeric()
->minValue(1)
->label(__('construction-equipment.construction_equipment_resource.serial_number'))
->unique(ignoreRecord: true)
->required()
->maxLength(255)
->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
return static function (string $attribute, $value, Closure $fail) use ($get, $component) {
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->exists();

if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
};
}),
]),
Tables\Actions\AttachAction::make()
->recordSelect(
fn (Select $select) => $select->noSearchResultsMessage(__('equipment.equipment_resource.search_field')),
)
->allowDuplicates()
->color('success')
->preloadRecordSelect()
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('serial_number')
->numeric()
->minValue(1)
->label(__('construction-equipment.construction_equipment_resource.serial_number'))
->unique(ignoreRecord: true)
->required()
->maxLength(255)
->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
return static function (string $attribute, $value, Closure $fail) use ($get, $component) {
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->exists();

if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
};
}),
]),
3 Replies
403gtfo
403gtfoβ€’13mo ago
This is on my list of 'learn how to do as well.
I have a M-M attachment with extra data on the pivot so it needs to allow for multiple connections. I have a feeling it would be something around injecting or convincing the ' $action->getRecordSelect()' to allow duplicates. There is probably a dead easy way to do it I haven't seen. That's next weeks investigation I hope πŸ™‚
Gush
Gushβ€’13mo ago
same problem here, boosting for awnser :))
403gtfo
403gtfoβ€’12mo ago
I managed to get duplicates to work. The doco is incomplete. 1. On the relation manager add this to the top of the class: protected bool $allowsDuplicates = true; 2. And then on the 'return $table' add this ->allowDuplicates() Hope that helps πŸ™‚
No description
No description
Want results from more Discord servers?
Add your server