column data in exporter

this is the code.

ExportColumn::make('data')->label('Data')
    ->formatStateUsing(function ($state) {
                    // Debug log the incoming state
                    \Log::info('Data State:', ['state' => $state, 'type' => gettype($state)]);
            
                    if (is_null($state)) {
                        return json_encode([]);
                    }
            
                    // Keep it simple: just encode the data as JSON
                    if (is_array($state)) {
                        return json_encode($state);
                    }
            
                    // If it's already a JSON string, return it
                    if (is_string($state)) {
                        // Check if it's valid JSON
                        json_decode($state);
                        if (json_last_error() === JSON_ERROR_NONE) {
                            return $state;
                        }
                    }
            
                    // For other types, convert to string
                    return (string)$state;
                }),


the output should be this
{"featured":true,"priority":1,"weather_resistant":true,"materials":["wood","metal","synthetic"]}


but i am getting only this.
true, 1, true, ["wood","metal","synthetic"]


i am getting the values and not the key.

in the resource it is like this.

                                    Forms\Components\KeyValue::make('data')
                                        ->label(__('Additional Details'))
                                        ->columnSpanFull(),
Was this page helpful?