Getting wrong data for `count(*)` field π’
For a table query, I call
select()
to specify a handful of non-standard "fields". One of them is specified like this: DB::raw('count(*) as current_result')
Then, I define the column like so: Tables\Columns\TextColumn::make('current_result')
The column appears, but every value is 1.
I'm using laravel debugbar and I can copy the query that is executed and it generates a bunch of results for current_result
that are not 1.
Do I have to do something special to set up a column for such data...? I feel like I'm missing something, but I can't figure it out. π³2 Replies
Post everything in detail. If you do now show the code you have written, nobody will understand what is the problem you are facing.
In case you want to show the count of relationship data, then you can just use the count() method like this:
TextColumn::make('students_count')->counts('students'),
It may not be easy to post everything in detail, but I can share a few points fairly easily.
First, I have an eloquent model called
Genage
and I pass the following query to my table's query()
method:
When it runs, all of the data looks good except for current_result
.
However, the SQL that is generated works perfectly fine when I run it on its own in my MySQL client, TablesPlus. (This is the same from debugbar or the query log.)
In TablesPlus, current_result
is never 1. When run within the Laravel/Filament app, it's always 1.