F
Filament4mo ago
zemo

$get('../../parent_field_name') is used to get a field outside the repeater, but what's the inverse?

in case I want to access a field inside the repeater, from outside the repeater. I tried something like this $get('relationship_name/item1/field_name') but it doesn't work Thanks
7 Replies
LeandroFerreira
LeandroFerreira4mo ago
$get('repeater_name') will return the repeater state (array)
zemo
zemoOP4mo ago
so if I want to access one of its properties I can just do $get('repeater_name')['property_name'] ? how would it work with $set instead? if I want to set a property inside the repeater array, from outside the repeater. thanks Leandro
toeknee
toeknee4mo ago
No because you need to loop through the results it would be:
foreach($get('repeater_name') as $item) {
$value = $item['property_name'];
}
foreach($get('repeater_name') as $item) {
$value = $item['property_name'];
}
You cant's set inside from outside a specific field inside, but you can get each item, loop through then update and set the new array.
zemo
zemoOP4mo ago
oh right, that was a naive error, thank you can I avoid the foreach? I only need to access the first item. Would $get('repeater_name')[0]['property_name'] work?
toeknee
toeknee4mo ago
Not like that, you need to select first and you don't know the id so:
$item = data_get(array_shift($get('repeater_name')), 'property_name', '');
$item = data_get(array_shift($get('repeater_name')), 'property_name', '');
zemo
zemoOP4mo ago
hey, thank you. I've tried doing this:
->afterStateUpdated(function (Get $get, Set $set, string $state) {
$set('payments', [
[
'payment_method_id' => data_get(array_shift($get('payments')), 'payment_method_id', ''),
'custom_date' => data_get(array_shift($get('payments')), 'custom_date', ''),
'status' => data_get(array_shift($get('payments')), 'status', ''),
'payment_reason_id' => 1,
'subscription_year_id' => $state,
'price' => $get('price'),
]
]);
})
->afterStateUpdated(function (Get $get, Set $set, string $state) {
$set('payments', [
[
'payment_method_id' => data_get(array_shift($get('payments')), 'payment_method_id', ''),
'custom_date' => data_get(array_shift($get('payments')), 'custom_date', ''),
'status' => data_get(array_shift($get('payments')), 'status', ''),
'payment_reason_id' => 1,
'subscription_year_id' => $state,
'price' => $get('price'),
]
]);
})
but it throws the error: "only variables should be passed by reference"
Dennis Koch
Dennis Koch4mo ago
array_shift() modifies the passed array, so you need to store it in a method first. You can't directly use $get()
Want results from more Discord servers?
Add your server