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 ?? [])),
])
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"]}]"
1 Reply
THY
THY3d ago
Maybe try to remove the outer [] before passing it to the query() function? {"shift_id":1,"shift_type":"TestType","shift_description":"TestDescription","shift_duration":25,"shift_date":"2024-11-27","shift_supervisor":["John Doe"]} The answer I am giving is from here (I think it may help you): https://github.com/filamentphp/filament/discussions/11140
GitHub
How can I display json data in a table · filamentphp filament · Dis...
Package Table builder Package Version v3 How can we help you? Hi, there is a market_position field in the table. It stores json [{"symbol":"BTCUSDT","side":"long/...
Want results from more Discord servers?
Add your server