FilamentPHP Custom table value from relationship

32 Views Asked by At

I have a Profile Model with HasMany educationalBackgrounds. I'm trying to display custom values with a foreach loop using TextColumn and getStateUsing() but it is not showing all records that I need.

TextColumn::make('educationalBackgrounds')
                ->getStateUsing( function (Profile $record){
                    foreach ($record->educationalBackgrounds as $record) {
                        return $record->level.' - '.$record->school_name.' - '.$record->degree_course.' - '.$record->year_graduated.' ; ';
                    }
                 })

It should show records from all levels (Elementary, high school and college) and only elementary records are showing.

1

There are 1 best solutions below

0
KrutSednar On BEST ANSWER

Found the solution. Here's the code:

TextColumn::make('educationalBackgrounds')
                ->getStateUsing( function (Profile $record){
                    $education = '';
                    foreach ($record->educationalBackgrounds as $key => $record) {
                        $education .= $record->level.' - '.$record->school_name.' - '.$record->degree_course.' - '.$record->year_graduated.' ; ';
                    }

                    return $education;
                 })