Search code examples
laravellaravel-livewirefilamentphp

FilamentPHP Custom table value from relationship


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.


Solution

  • 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;
                     })