F
Filament15mo ago
CodeMax

Show Repeaters first entry in Table View

I would like to show an entry in the Table View of Component. How can i access this data? This is my repeater:
Forms\Components\Repeater::make('dates')
->label("Termine")
->schema([
Forms\Components\Select::make('location')
->label('Ort')
->required()
->relationship('location', 'title'),
Forms\Components\DatePicker::make('date')
->timezone('Europe/Berlin')
->label("Datum")
->required(),
Forms\Components\TimePicker::make('start_time')
->timezone('Europe/Berlin')
->seconds(false)
->label("Startzeit")
->required(),
Forms\Components\TimePicker::make('end_time')
->timezone('Europe/Berlin')
->seconds(false)
->label("Endzeit")
->required(),

])
->columns(2)
->defaultItems(1)
->cloneable()
Forms\Components\Repeater::make('dates')
->label("Termine")
->schema([
Forms\Components\Select::make('location')
->label('Ort')
->required()
->relationship('location', 'title'),
Forms\Components\DatePicker::make('date')
->timezone('Europe/Berlin')
->label("Datum")
->required(),
Forms\Components\TimePicker::make('start_time')
->timezone('Europe/Berlin')
->seconds(false)
->label("Startzeit")
->required(),
Forms\Components\TimePicker::make('end_time')
->timezone('Europe/Berlin')
->seconds(false)
->label("Endzeit")
->required(),

])
->columns(2)
->defaultItems(1)
->cloneable()
25 Replies
bwurtz999
bwurtz99915mo ago
I'm not sure I completely understand your question, but if you are trying to access data from within the repeater you need to use something like $get(../formField) to go one level up from the repeater If you're trying to access data from outside the repeater, you would use $get('dates') and then that would give you an array of all the repeaters, which you could use a loop to go through and pull data
CodeMax
CodeMaxOP15mo ago
Thanks for the reply. I would like to use it here in the TABLE VIEW. But sth. like dates[0].date is not working as it cannot read the array here this way
No description
bwurtz999
bwurtz99915mo ago
Oh... I don't know if that is possible Can you explain more about what you are trying to accomplish? There might be a different way to do it
awcodes
awcodes15mo ago
->formatStateUsing() you can grab the first item in the array and return it as the state for the column.
CodeMax
CodeMaxOP15mo ago
@awcodes thanks - can you show me how this is done in my example? I only get an "array to string" conversion error. my dates json field looks like this:
[
{
"date": "2023-09-29",
"end_time": "20:35",
"location": "21",
"start_time": "18:34"
}
]
[
{
"date": "2023-09-29",
"end_time": "20:35",
"location": "21",
"start_time": "18:34"
}
]
awcodes
awcodes15mo ago
In your case $state is the array.
CodeMax
CodeMaxOP15mo ago
So it should be like this: ->formatStateUsing(fn (string $state): string => __("dates.{$state['date']}")) but is does not return anything @awcodes
No description
awcodes
awcodes15mo ago
No. And why are you trying to translate the date? Just return $state[0][‘date’]
CodeMax
CodeMaxOP15mo ago
Still no return. Is still empty even though there's an entry in the db
awcodes
awcodes15mo ago
dd($state) what does it show.
CodeMax
CodeMaxOP15mo ago
That is the weird part. Errors are executed and shown, but i can not dd nor Debugbar::info() anything a this point. I am within a RelationsManager. Might this be part of the problem?
awcodes
awcodes15mo ago
No, it’s still a table Make sure you have the appropriate cast on your model too.
CodeMax
CodeMaxOP15mo ago
is cast as array the return you sent is missing the dates isn't it?
awcodes
awcodes15mo ago
No dates it’s the name of the field. $state is the vale of dates.
CodeMax
CodeMaxOP15mo ago
Then i get the error Array to string conversion if i name the field "dates"
awcodes
awcodes15mo ago
Unless dates is a key in a larger json structure that you aren’t showing.
CodeMax
CodeMaxOP15mo ago
dates is the name of the repeater Forms\Components\Repeater::make('dates'). see first message
awcodes
awcodes15mo ago
Right, but the names of the form field should be the same as the column on your model. And it’s the same for tables.
CodeMax
CodeMaxOP15mo ago
No description
awcodes
awcodes15mo ago
So $state is the value of that column Your form looks fine.
CodeMax
CodeMaxOP15mo ago
if i name the column dates i get the array to string error
No description
CodeMax
CodeMaxOP15mo ago
No description
awcodes
awcodes15mo ago
So, dd($state[0][‘date’]) I really can’t debug for you. 🙂
CodeMax
CodeMaxOP15mo ago
i know. but debugging is not working. As i wrote… as soon as i name the TextColumn dates i get the array to string error. If i name it differntly i can not dd... Filament does not let me name the Table Field the same as the Form field... array to string error @awcodes this is working btw:
Tables\Columns\TextColumn::make('dates')
->getStateUsing(function ($record) {
return $record->dates[0]['date'];
})
->label("Datum"),
Tables\Columns\TextColumn::make('dates')
->getStateUsing(function ($record) {
return $record->dates[0]['date'];
})
->label("Datum"),
Want results from more Discord servers?
Add your server