F
Filament8mo ago
Daniel

how to use set in a repeater

I have a select called hospital_id, and within the same form I have a tab that inside there is a repeater called procedures, how do I modify the value of the repeater with $set when I change the hospital select (which is outside the tab )
No description
8 Replies
Daniel
DanielOP8mo ago
this is my select:
Forms\Components\Select::make('hospital_id')
->live()
->afterStateUpdated(function (Set $set, Get $get, ? string $state, ?string $old) {

$set('procedures/procedures/0/procedure_value','2');
})
->label(__('Hospital'))
->relationship('hospital', 'name')
->required(),

Forms\Components\Select::make('hospital_id')
->live()
->afterStateUpdated(function (Set $set, Get $get, ? string $state, ?string $old) {

$set('procedures/procedures/0/procedure_value','2');
})
->label(__('Hospital'))
->relationship('hospital', 'name')
->required(),

and this is my tab with the repeater inside:
Tabs\Tab::make('Procedures')
->live()
->hidden(function(Get $get){
if(! empty($get('anesthetist_group_id')) && ! empty($get('hospital_id'))){
return false;
}
return true;
})
->schema([
Repeater::make('procedures')
->columns(3)
->schema([
Select::make('procedure_id')
->options(Procedure::all()->pluck('name','id'))
->live()
->afterStateUpdated(function (Set $set, Get $get, ?string $state, ?string $old) {
$hospital = Hospital::find($get('../../hospital_id'));
$anesthetistGroup = AnesthetistGroup::find($get('../../anesthetist_group_id'));

$procedureValue = ProcedureValue::where('hospital_id',$hospital->id)
->where('anesthetist_group_id',$anesthetistGroup->id)
->where('procedure_id',$state);

if($procedureValue->exists()){
$set('procedure_value',$procedureValue->first()->value);
}else{
Notification::make('')
->title('O procedimento não existe para esse grupo de anestesista neste hospital')

->warning()
->send()}})
Tabs\Tab::make('Procedures')
->live()
->hidden(function(Get $get){
if(! empty($get('anesthetist_group_id')) && ! empty($get('hospital_id'))){
return false;
}
return true;
})
->schema([
Repeater::make('procedures')
->columns(3)
->schema([
Select::make('procedure_id')
->options(Procedure::all()->pluck('name','id'))
->live()
->afterStateUpdated(function (Set $set, Get $get, ?string $state, ?string $old) {
$hospital = Hospital::find($get('../../hospital_id'));
$anesthetistGroup = AnesthetistGroup::find($get('../../anesthetist_group_id'));

$procedureValue = ProcedureValue::where('hospital_id',$hospital->id)
->where('anesthetist_group_id',$anesthetistGroup->id)
->where('procedure_id',$state);

if($procedureValue->exists()){
$set('procedure_value',$procedureValue->first()->value);
}else{
Notification::make('')
->title('O procedimento não existe para esse grupo de anestesista neste hospital')

->warning()
->send()}})
I'm using
$set('procedures/procedures/0/procedure value','2');
$set('procedures/procedures/0/procedure value','2');
inside the hospital select to try to get to the repeater, but I couldn't I solved it using
use Illuminate\Support\Arr;
Forms\Components\Select::make('hospital_id')
->live()
->afterStateUpdated(function (Set $set, Get $get, ?string $state, ?string $old) {
$procedures = $get('procedures');
[$keys , $values] = Arr::divide($procedures);

$set("procedures.{$keys[0]}.procedure_value",'2');

})
->label(__('Hospital'))
->relationship('hospital', 'name')
->required(),
use Illuminate\Support\Arr;
Forms\Components\Select::make('hospital_id')
->live()
->afterStateUpdated(function (Set $set, Get $get, ?string $state, ?string $old) {
$procedures = $get('procedures');
[$keys , $values] = Arr::divide($procedures);

$set("procedures.{$keys[0]}.procedure_value",'2');

})
->label(__('Hospital'))
->relationship('hospital', 'name')
->required(),
Daniel
DanielOP8mo ago
Hi, the documentation teaches how to get the value from inside the repeater, I needed the opposite (get the information from outside the repeater)
jepewsykes
jepewsykes8mo ago
using array to set repeater $set('key',$array) array should contains repeater field and value
Daniel
DanielOP8mo ago
yes yes 😁
jepewsykes
jepewsykes8mo ago
Okay
Hung Thai
Hung Thai4mo ago
what do you mean by this? for ex, I have a structure like: -- TextFather: Name -- Repeater: ----Text-Input How can I set the Text-Input inside the Repeater with the value of TextFather (which is Name)?
jepewsykes
jepewsykes4mo ago
u can use afterstateupdated and set('field',$array_contain_textinput_name_and_value)
Want results from more Discord servers?
Add your server