Apply changes to field's sibling from Repeater
Using a column called
is_primary_contact
to a Parent model, I want to use $set() to apply changes to all of the parents. There can be only 1 primary contact, so If I set the parent1 to is_primary_contact
, the rest of the parents should be false
.
Select::make('is_primary_contact')
->label('Is this parent the Primary Contact? (Only 1 parent can be the Primary Contact)')
->options([
1 => 'Yes',
0 => 'No'
])
->required()
->reactive()
->afterStateUpdated(function(Closure $get, Closure $set, $state){
if($state == 1)
{
$parentsRepeater = $get('../../parents');
foreach($parentsRepeater as $repeaterItemUuid => $parentItem)
{
// How??
}
}
$this->autoSaveParent($get('id'),'is_primary_contact', $state);
}),
Select::make('is_primary_contact')
->label('Is this parent the Primary Contact? (Only 1 parent can be the Primary Contact)')
->options([
1 => 'Yes',
0 => 'No'
])
->required()
->reactive()
->afterStateUpdated(function(Closure $get, Closure $set, $state){
if($state == 1)
{
$parentsRepeater = $get('../../parents');
foreach($parentsRepeater as $repeaterItemUuid => $parentItem)
{
// How??
}
}
$this->autoSaveParent($get('id'),'is_primary_contact', $state);
}),
1 Reply
I think this one is working
->afterStateUpdated(function(Closure $get, Closure $set, $state){
if($state == 1)
{
$parentsRepeater = $get('../../parents');
foreach($parentsRepeater as $repeaterItemUuid => $parentItem){
if($get('id') != $parentItem['id'])
{
# Backend
$parentModel = ParentModel::find($parentItem['id']);
$parentModel->is_primary_contact = false;
$parentModel->save();
# Frontend
$set('../../parents.'. $repeaterItemUuid.'.is_primary_contact', 0);
}
}
}
$this->autoSaveParent($get('id'),'is_primary_contact', $state);
}),
->afterStateUpdated(function(Closure $get, Closure $set, $state){
if($state == 1)
{
$parentsRepeater = $get('../../parents');
foreach($parentsRepeater as $repeaterItemUuid => $parentItem){
if($get('id') != $parentItem['id'])
{
# Backend
$parentModel = ParentModel::find($parentItem['id']);
$parentModel->is_primary_contact = false;
$parentModel->save();
# Frontend
$set('../../parents.'. $repeaterItemUuid.'.is_primary_contact', 0);
}
}
}
$this->autoSaveParent($get('id'),'is_primary_contact', $state);
}),