F
Filament7d ago
br

Infolist Repeatable with custom state

With filamentphp 3 I try to render an entity history using infolist repeatable with text entries for field name, old value and new value. Though I am not able to get my field values shown. It seems like the number of repititions is correct (2). This is my code:
RepeatableEntry::make('mydata')
->label('Field Changes')
->columnSpanFull()

->state([
//'mydata' => [
[
'field_name' => 'a field',
'old_value' => 'an old value',
'new_value' => 'a new value',
],
[
'field_name' => 'another field',
'old_value' => 'another old value',
'new_value' => 'another new value',
],
//]
])

->schema([
TextEntry::make('field_name'),
TextEntry::make('old_value'),
TextEntry::make('new_value'),
]),
RepeatableEntry::make('mydata')
->label('Field Changes')
->columnSpanFull()

->state([
//'mydata' => [
[
'field_name' => 'a field',
'old_value' => 'an old value',
'new_value' => 'a new value',
],
[
'field_name' => 'another field',
'old_value' => 'another old value',
'new_value' => 'another new value',
],
//]
])

->schema([
TextEntry::make('field_name'),
TextEntry::make('old_value'),
TextEntry::make('new_value'),
]),
What is the correct approach to make this work?
2 Replies
Romain 'Maz' B.
Hey! I am trying to solve the same problem right now to displays a list of images, did you find a solution?
RepeatableEntry::make('images_urls')
->schema([
ImageEntry::make('url'),
])
// Convert the list of urls into a two dimensional keyed array
->getStateUsing(
fn (Prompt $prompt) => array_map(
fn (string $url) => ['url' => $url],
$prompt->images_urls
))
);
RepeatableEntry::make('images_urls')
->schema([
ImageEntry::make('url'),
])
// Convert the list of urls into a two dimensional keyed array
->getStateUsing(
fn (Prompt $prompt) => array_map(
fn (string $url) => ['url' => $url],
$prompt->images_urls
))
);
br
brOP6d ago
Hey! No, not really. I decided to go another path and created my own filament component. Thats probably a little bit oversized but solves my problem:
ChangeHistory::make('mydata')
->label('Change History')
->columnSpanFull(),
ChangeHistory::make('mydata')
->label('Change History')
->columnSpanFull(),
<?php

namespace App\Infolists\Components;

use Filament\Infolists\Components\Entry;

class ChangeHistory extends Entry
{
protected string $view = 'infolists.components.change-history';

public function getState(): mixed
{
return parent::getState() ?? [];
}
}
<?php

namespace App\Infolists\Components;

use Filament\Infolists\Components\Entry;

class ChangeHistory extends Entry
{
protected string $view = 'infolists.components.change-history';

public function getState(): mixed
{
return parent::getState() ?? [];
}
}
<x-dynamic-component :component="$getEntryWrapperView()" :entry="$entry">
<div>
@forelse($getState() as $version)
<!-- some code here -->
@foreach($version['changed_fields'] as $field)
<!-- some code here -->
@endforeach
@empty
<!-- some code here -->
@endforelse
</div>
</x-dynamic-component>
<x-dynamic-component :component="$getEntryWrapperView()" :entry="$entry">
<div>
@forelse($getState() as $version)
<!-- some code here -->
@foreach($version['changed_fields'] as $field)
<!-- some code here -->
@endforeach
@empty
<!-- some code here -->
@endforelse
</div>
</x-dynamic-component>

Did you find this page helpful?