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
toeknee5mo ago
Can you provide the code and what you are seeing
abdullafahem
abdullafahemOP5mo 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
abdullafahemOP5mo ago
I tried with modifiying the query but does not work throwing errors
toeknee
toeknee5mo 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
abdullafahemOP5mo 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
toeknee5mo ago
TextColumn::make('stages.stage') Why don't you have the stage return the relationship latest element?
abdullafahem
abdullafahemOP5mo ago
I didn't understand
micraux
micraux5mo 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
abdullafahemOP5mo 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
micraux5mo 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
krekas5mo ago
What is this last()?
krekas
krekas5mo 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
abdullafahem5mo 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
micraux5mo 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