F
Filamentβ€’3mo ago
Pekempy

(Beginner question) Repeater - Formatting the output after update

On the Repeater, I want to be able to display how they will appear in the live view using the Textarea below. In the screenshot example, I want to change it to be: Idina Menzel (Elphaba), Kristen Chenoweth (Glinda) and display that in the Cast field below. I also want to save the data in Cast to the database, rather than using the Repeater data directly.
No description
Solution:
@Pekempy but if you save only the Cast to the database, then your Edit form would not work with Repeater, I don't think it's a good decision. And with filling in the Cast, use the afterStateUpdated() method probably. Maybe you are already doing that, but you haven't shown any code, so hard to comment....
Jump to solution
9 Replies
awcodes
awcodesβ€’3mo ago
Does β€˜cast’ exist as a field on the model?
Solution
Povilas K
Povilas Kβ€’3mo ago
@Pekempy but if you save only the Cast to the database, then your Edit form would not work with Repeater, I don't think it's a good decision. And with filling in the Cast, use the afterStateUpdated() method probably. Maybe you are already doing that, but you haven't shown any code, so hard to comment.
Pekempy
Pekempyβ€’3mo ago
Hm that's true - So I should store the cast_list_entries into the table as a json perhaps? Issue there is I have a lot of legacy data which I'm importing so I'd have to build the json for them all in the importer too πŸ€”
Povilas K
Povilas Kβ€’3mo ago
well yeah, I guess it's part of the job of working with legacy data that you may want to still work in the future
Pekempy
Pekempyβ€’3mo ago
This is my repeater / textarea in the resource currently
Repeater::make('cast_list_entries')
->label('Cast Members')
->schema([
Select::make('performer_id')
->relationship('performer', 'name')
->getSearchResultsUsing(function (string $search) {
return Performer::search($search)
->pluck('name', 'id');
})
->searchable()
->required()
->columnSpan(4),
Select::make('character_id')
->relationship('character', 'name')
->searchable()
->getSearchResultsUsing(function (string $search) {
$showId = session('show_id');
return Character::search($search)
->where('show_id', $showId)
->pluck('name', 'id');
})
->required()
->columnSpan(4),
Select::make('status')
->label('Status')
->placeholder('Principal')
->options(CastStatus::class)
->columnSpan(2),
])
->reorderable(false)
->reorderableWithDragAndDrop(false)
->collapsible()
->hint("If incorrect 'Character' are showing, make sure you've selected the correct 'Show' above.")
->columns(10)
->columnSpanFull(),

Textarea::make('cast')
->label('Cast')
->live()
->autosize()
->required()
->helperText('The order of the cast members may be changed for consistency with other recordings of this show.')
->columnSpanFull(),
])
Repeater::make('cast_list_entries')
->label('Cast Members')
->schema([
Select::make('performer_id')
->relationship('performer', 'name')
->getSearchResultsUsing(function (string $search) {
return Performer::search($search)
->pluck('name', 'id');
})
->searchable()
->required()
->columnSpan(4),
Select::make('character_id')
->relationship('character', 'name')
->searchable()
->getSearchResultsUsing(function (string $search) {
$showId = session('show_id');
return Character::search($search)
->where('show_id', $showId)
->pluck('name', 'id');
})
->required()
->columnSpan(4),
Select::make('status')
->label('Status')
->placeholder('Principal')
->options(CastStatus::class)
->columnSpan(2),
])
->reorderable(false)
->reorderableWithDragAndDrop(false)
->collapsible()
->hint("If incorrect 'Character' are showing, make sure you've selected the correct 'Show' above.")
->columns(10)
->columnSpanFull(),

Textarea::make('cast')
->label('Cast')
->live()
->autosize()
->required()
->helperText('The order of the cast members may be changed for consistency with other recordings of this show.')
->columnSpanFull(),
])
yeah I'll have a look at storing this and how its formatted and work on the legacy. and afterStateUpdated too, thank you!
Pekempy
Pekempyβ€’3mo ago
Got a small issue with the loading on the edit page - seems to be pilling the REpeater Selects with the ID of the performer/character, rather than the name. It works perfectly on the Create page though πŸ€”
Povilas K
Povilas Kβ€’3mo ago
But also, it depends on how exactly do you save that json in the table
Pekempy
Pekempyβ€’3mo ago
Thanks, I'll take a look. It's storing just as [{"status": "swing", "character": "5", "performer": "48"}] And in the model:
public function getCastListEntriesAttribute($value)
{
$castListEntries = json_decode($value, true);

return $castListEntries;
}
public function setCastListEntriesAttribute($value)
{
$this->attributes['cast_list_entries'] = json_encode($value);
}
public function getCastListEntriesAttribute($value)
{
$castListEntries = json_decode($value, true);

return $castListEntries;
}
public function setCastListEntriesAttribute($value)
{
$this->attributes['cast_list_entries'] = json_encode($value);
}
Seems to work loading the label if I use Performer::pluck->('name','id'), but with thousands that's a bit slow. For character I have added a query for the show_id but unable to do that on the performer πŸ€” Solved with: ->getOptionLabelUsing(fn($value): ?string => Performer::find($value)?->name)
Want results from more Discord servers?
Add your server
More Posts