TableWidget with morphTo and groupedBy
I have a Eloquent model
StatisticsClick
with the following columns:
- id
- model_type
- model_id
- created_at
I have defined a morphTo relationshop:
The morphTo goes (for now) only to the Product
model.
For this, I wanna create a widget to show the most clicked elements, with the name and the counted number of StatisticsClick
entries. Something like this result:
| name | count |
|-----------|-------|
| Product 3 | 44 |
| Product 1 | 32 |
| Product 2 | 18 |
To get this, I would write:
The problem is, that a table widgets getTableQuery
only accept Illuminate\Database\Eloquent\Builder
as return values and no Illuminate\Database\Query\Builder
. This is my starting point:
This shows me the table with a list of all clicks, by the name of the related product. But how to group now after product.id and get the count?
Any ideas?2 Replies
@bernhard86 Not exactly a direct answer to you question but, have you heard of Sushi?
https://filamentphp.com/blog/how-to-consume-an-external-api-with-filament-tables
This article explains how to use it to query an external API and present the results in a Filament table. I've also used it to "wrap" a custom
DB::query()
(instead of an API), to further process and group some data. Maybe this can be useful for your use-case.I'm sorry i would need some help, because i have the same problem.
I have a notes table (i have attached the structure), each note can be set to highlight. I created a relation in model Note, it goes only to Appointment for now. I need to create a Table widget for show only highlights note (i attach the widget's structure ), but the model.id columns is empty.
What's wrong with what I am doing?