How to properly use the listWithLineBreaks method from TextColumn

Heya guys, i've been playing around with the TextColumn listWithLineBreaks function paired with some JSON data. I store this data in the database:
[{"type":"LAPTOP","data":{"Merk":"Bakkers Studio Stichting","Model":"et064","CPU":"Intel i5","RAM":"16GB","Opslag":"1TB HDD"}}]
[{"type":"LAPTOP","data":{"Merk":"Bakkers Studio Stichting","Model":"et064","CPU":"Intel i5","RAM":"16GB","Opslag":"1TB HDD"}}]
which is being casted to an array from the model
protected function casts(): array
{
return [
'specs' => 'array',
];
}
protected function casts(): array
{
return [
'specs' => 'array',
];
}
Now I would like to have all the data from data to be displayed in a table using the listWithLineBreaks function. I assumed that this would need an array to work with. So, my assumption would be that:
TextColumn::make('specs')
->label('specs')
->searchable()
->listWithLineBreaks()
->formatStateUsing(function ($state) {
if (empty($state['data'])) {
return '-';
}

return $state['data'];
})
TextColumn::make('specs')
->label('specs')
->searchable()
->listWithLineBreaks()
->formatStateUsing(function ($state) {
if (empty($state['data'])) {
return '-';
}

return $state['data'];
})
Would properly display the items in a list format. but It does not. So, i've tried supplying the 'listWithLineBreaks' not with an array, but with a string by implode:
TextColumn::make('specs')
->label('specs')
->searchable()
->listWithLineBreaks()
->separator(';')
->formatStateUsing(function ($state) {
if (empty($state['data'])) {
return '-';
}

return implode(';', $state['data']);
}),
TextColumn::make('specs')
->label('specs')
->searchable()
->listWithLineBreaks()
->separator(';')
->formatStateUsing(function ($state) {
if (empty($state['data'])) {
return '-';
}

return implode(';', $state['data']);
}),
But that also results in a single string being displayed and not being separated on the ';'. A workaround is to use the html() function and add <br/> to the output string but i'd rather use the function above. What am I missing out here?
1 Reply
LeandroFerreira
I think you need to use state() instead of formatStateUsing What would be the result that you need? key: value? or this?
TextColumn::make('specs')
->state(fn (YourModel $record) => data_get(Arr::first($record->specs), 'data'))
->listWithLineBreaks()
TextColumn::make('specs')
->state(fn (YourModel $record) => data_get(Arr::first($record->specs), 'data'))
->listWithLineBreaks()

Did you find this page helpful?