FilamentF
Filament15mo ago
3 replies
Oxygarum

Access JSON and display in a table

Hey, probably I am overlooking something really obvious.
I have a table and in that want to access a JSON that is stored in a database column in another model.
When trying to debug with log::info it is displaying me the info correctly, but I am not sure how to let ->query use the data in the JSON

My code for accessing the JSON looks like this:

public static function table(Table $table): Table
    {
        $hrRecord = Hrdatabase::where('badgeofemployee', $badgeNumber)->first();
        
        
$visitedShifts = json_decode($hrRecord->visitedshifts, true);

Log::info('Visited Shifts: ', $visitedShifts);

        

        return $table
        ->query($visitedShifts)
        
            ->columns([
                Tables\Columns\TextColumn::make('shift_date')
                    ->label('Date of Shift')
                    ->getStateUsing(fn ($record) => $record->shift_date ?? 'N/A'),

                Tables\Columns\TextColumn::make('shift_type')
                    ->label('Shift Type')
                    ->getStateUsing(fn ($record) => $record->shift_type ?? 'N/A'),

                Tables\Columns\TextColumn::make('shift_description')
                    ->label('Shift Description')
                    ->getStateUsing(fn ($record) => $record->shift_description ?? 'N/A'),

                Tables\Columns\TextColumn::make('shift_duration')
                    ->label('Shift Duration')
                    ->getStateUsing(fn ($record) => $record->shift_duration ?? 'N/A'),

                Tables\Columns\TextColumn::make('shift_supervisor')
                    ->label('Shift Supervisor')
                    ->getStateUsing(fn ($record) => implode(', ', $record->shift_supervisor ?? [])),
            ])



For the log::info I receive back my test entry of
"[{"shift_id":1,"shift_type":"TestType","shift_description":"TestDescription","shift_duration":25,"shift_date":"2024-11-27","shift_supervisor":["John Doe"]}]"
Was this page helpful?