Repeater Fields to TextColumn

Hi, I have a Repeater with one field stage_name and I need to display it in table but when I display it shows all stages name how to show only the last stage name? Thank you!
Solution:
Thank you guys for all answers I just solved with Laravel Attribute (Casts) function and made all logic inside and it works perfectly.
Jump to solution
14 Replies
toeknee
toeknee6mo ago
Can you provide the code and what you are seeing
abdullafahem
abdullafahemOP6mo ago
Like this I need to display only the last one this is in table and it came from the repeater fields
No description
abdullafahem
abdullafahemOP6mo ago
I tried with modifiying the query but does not work throwing errors
toeknee
toeknee6mo ago
we need to see the code you are using? otherwise
->formatStateUsing(fn (string $state): string => array_pop($state)))
->formatStateUsing(fn (string $state): string => array_pop($state)))
abdullafahem
abdullafahemOP6mo ago
Let me try this TextColumn::make('stages.stage.name') ->label('المرحلة') ->formatStateUsing(fn (string $state): string => array_pop($state)) ->badge(), It throws error: array_pop(): Argument #1 ($array) must be of type array, string given
toeknee
toeknee6mo ago
TextColumn::make('stages.stage') Why don't you have the stage return the relationship latest element?
abdullafahem
abdullafahemOP6mo ago
I didn't understand
micraux
micraux6mo ago
Hi, Your mistake is using array_pop() which apply to arrays, because the result is a collection and not an array. Try something like this:
TextColumn::make('stages')
->formatStateUsing(fn (YourModel $record): string =>
$record->stages->last()->name
)
TextColumn::make('stages')
->formatStateUsing(fn (YourModel $record): string =>
$record->stages->last()->name
)
abdullafahem
abdullafahemOP6mo ago
Let me try I appreciate it for your answer.
It throws me an error: Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::last()
micraux
micraux6mo ago
Ok, so try to display the content of $state and tell us what you have:
TextColumn::make('stages.stage.name')
->formatStateUsing(fn (string $state): string => $state)
TextColumn::make('stages.stage.name')
->formatStateUsing(fn (string $state): string => $state)
krekas
krekas6mo ago
What is this last()?
krekas
krekas6mo ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Solution
abdullafahem
abdullafahem6mo ago
Thank you guys for all answers I just solved with Laravel Attribute (Casts) function and made all logic inside and it works perfectly.
micraux
micraux6mo ago
You may have noticed that I mentioned Collection, so here: https://laravel.com/docs/11.x/collections#method-last
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Want results from more Discord servers?
Add your server