Custom Column

Hi guys, I would like to create a custom column value, but don't know how ? It's a "complex" query (and I have many like that), but don't know how to inject the code on it.
Tables\Columns\TextColumn::make('feedbacks_count')->formatStateUsing(fn (string $state): string => 'my_custom_query'),
Tables\Columns\TextColumn::make('feedbacks_count')->formatStateUsing(fn (string $state): string => 'my_custom_query'),
Solution:
Finally @awcodes I find the ->getStateUsing() method that work as well!
Jump to solution
29 Replies
ChesterS
ChesterS13mo ago
I'm not sure what you're trying to do but you can pass a custom view to the column
Tables\Columns\TextColumn::make('feedbacks_count')->view(),
Tables\Columns\TextColumn::make('feedbacks_count')->view(),
SUNSHINE
SUNSHINEOP13mo ago
Hmm, it's just a query that I have with a result at the end. I want to display this value in the column. View is to display a view, no ? I just want to display a count from an advanced query result. MyModel::query()->...->count()
ChesterS
ChesterS13mo ago
I'm still not sure what you're trying to do :/ Where does this query come from? You can pass the record if you need formatStateUsing(function (YourModel $record) { ... } If you just want a query, you can do that too, but it will run for each row
SUNSHINE
SUNSHINEOP13mo ago
Yep, it should be run for each row. It's just a column where I should execute a query to count ALL relations from the ID (but I can't use ->relationships(), it's more advanced)
ChesterS
ChesterS13mo ago
Ok then you can pass the record and then run a custom query using the ID
SUNSHINE
SUNSHINEOP13mo ago
Yep, it's what I want, but how?
ChesterS
ChesterS13mo ago
eg
formatStateUsing(function (YourModel $record) { YourModel::where('something', $record->id)->get();
})
formatStateUsing(function (YourModel $record) { YourModel::where('something', $record->id)->get();
})
SUNSHINE
SUNSHINEOP13mo ago
->formatStateUsing(fn (Organization $record): string => $record->name) already what I do it don't work. (I put record->name, just for testing)
Tables\Columns\TextColumn::make('feedbacks_count')
->formatStateUsing(fn (Organization $record): string => $record->name),
Tables\Columns\TextColumn::make('feedbacks_count')
->formatStateUsing(fn (Organization $record): string => $record->name),
ChesterS
ChesterS13mo ago
ok then change that to the query you want, like the example above
SUNSHINE
SUNSHINEOP13mo ago
No but ->formatStateUsing() has no effect.
->formatStateUsing(fn (Organization $record): string => 'test')
->formatStateUsing(fn (Organization $record): string => 'test')
It's supposed to display "test" in the column, but it display anything.
ChesterS
ChesterS13mo ago
hmm that's weird. can you show the entire column? The code for the column
SUNSHINE
SUNSHINEOP13mo ago
Tables\Columns\TextColumn::make('feedbacks_count')->formatStateUsing(fn (Organization $record): string => 'test'),
Tables\Columns\TextColumn::make('feedbacks_count')->formatStateUsing(fn (Organization $record): string => 'test'),
nothing more
SUNSHINE
SUNSHINEOP13mo ago
No description
ChesterS
ChesterS13mo ago
Do you have a column feedbacks_count in your model/table?
SUNSHINE
SUNSHINEOP13mo ago
no I want to create a custom column.
ChesterS
ChesterS13mo ago
That might be why. Can change make('feedback_count) to something else to try it out? To a column that exists
SUNSHINE
SUNSHINEOP13mo ago
what that's a non-sense lol I have no more columns
ChesterS
ChesterS13mo ago
Haha sorry I mean to a column that exists on the model/table
SUNSHINE
SUNSHINEOP13mo ago
"Organization" just contain an id and a name nothing more.. exception relations.
ChesterS
ChesterS13mo ago
just use name again
SUNSHINE
SUNSHINEOP13mo ago
nop don't work the column disappear
ChesterS
ChesterS13mo ago
You can try a custom column I guess ¯\_(ツ)_/¯ Or create a relationship that returns the data you need - if that's possible
SUNSHINE
SUNSHINEOP13mo ago
nop impossible with relationship, it should be a query executed. custom column.. damn, that's strange that we cannot do a simple thing like this one 😄
ChesterS
ChesterS13mo ago
It's probably possible but I'm not familiar enough. You can try a dumb hack, like create an attribute on the model and then use formatStateUsing to overwrite the value Anyway, maybe someone can help more 😦
SUNSHINE
SUNSHINEOP13mo ago
Yep I hope SUPPORT team will join this thread! thx
awcodes
awcodes13mo ago
Would it not be simpler to add it as an attribute to the model. My biggest concern here would be creating a n+1 issue
Solution
SUNSHINE
SUNSHINE13mo ago
Finally @awcodes I find the ->getStateUsing() method that work as well!
awcodes
awcodes13mo ago
If you do it as an attribute on the model you could modify the table query to use ->with() so you get around the n+1 issue.
SUNSHINE
SUNSHINEOP13mo ago
Thanks! Nice catch.
Want results from more Discord servers?
Add your server