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.
29 Replies
I'm not sure what you're trying to do but you can pass a custom view to the column
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()
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 rowYep, 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)
Ok then you can pass the record and then run a custom query using the ID
Yep, it's what I want, but how?
eg
->formatStateUsing(fn (Organization $record): string => $record->name)
already what I do
it don't work.
(I put record->name, just for testing)
ok then change that to the query you want, like the example above
No but
->formatStateUsing()
has no effect.
It's supposed to display "test" in the column, but it display anything.hmm that's weird. can you show the entire column?
The code for the column
nothing more

Do you have a column
feedbacks_count
in your model/table?no
I want to create a custom column.
That might be why. Can change
make('feedback_count
) to something else to try it out? To a column that existswhat
that's a non-sense lol
I have no more columns
Haha sorry I mean to a column that exists on the model/table
"Organization" just contain an id and a name
nothing more.. exception relations.
just use
name
againnop don't work
the column disappear
You can try a custom column I guess ¯\_(ツ)_/¯
Or create a relationship that returns the data you need - if that's possible
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 😄
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 😦Yep I hope SUPPORT team will join this thread!
thx
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
Finally @awcodes I find the
->getStateUsing()
method that work as well!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.
Thanks! Nice catch.