Sortable based on average in related table #273
-
I have a column which uses an Eloquent accessor which averages a column on a related table. The column works fine but I want to be able to sort on that column. I do realize that it is not really possible to sort in the database on an accessor because accessors are only run after the query executes. So I guess I have to make the average in the query. How can I sort a column based on the average for a related hasMany relationship? Here is my table structure
variations
Here is my relationship on the Parent model
Here is my accessor on Parent model to get the average of its' variation prices
Here is how I generate the column using the accessor How can I make the sortable do the same thing as my accessor would do? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You can pass a callback to the sortable: https://github.com/rappasoft/laravel-livewire-tables/wiki/Using-the-built-in-sorting Might be able to do it with a subquery from there. |
Beta Was this translation helpful? Give feedback.
-
hello @eswachsman , i have the same case as you but i can not access my custom attribute in the component ?
|
Beta Was this translation helpful? Give feedback.
-
hello @eswachsman , i have the same case as you but i can not access my custom attribute in the component ?
|
Beta Was this translation helpful? Give feedback.
You can pass a callback to the sortable: https://github.com/rappasoft/laravel-livewire-tables/wiki/Using-the-built-in-sorting
Might be able to do it with a subquery from there.