nova-package-development icon indicating copy to clipboard operation
nova-package-development copied to clipboard

Sort by computed field

Open thiskbj opened this issue 6 years ago • 3 comments

Package idea: I'd love to be able to sort by computed field. Right now I'm just adding the field as a regular field, and periodically double-checking it (ew :). Anyone have a smarter idea?

thiskbj avatar Apr 04 '19 18:04 thiskbj

https://github.com/laravel/nova-issues/issues/85

So, there are SOME things you can do, as you can see in that issue above. But one of the authors of Nova, David Hemphill, also said it's going to be tough to do it without breaking pagination--probably because it's using MySQL-based sort and you can't do that with a PHP-computed property.

With your periodically double-checking option, if you get stuck there and no one can solve this particular issue, one option (and maybe this is what you mean with periodical double-check it) is to set a trigger--either a cron job, or every time an event happens that would impact the computation of this field--that then makes Laravel calculate it and save it to the regular computed field. Like a projection.

Not sure where that lines up with your specific use case. Like I mentioned in the previous issue, I hope we can solve this because I've definitely run into it before!

mattstauffer avatar Apr 04 '19 18:04 mattstauffer

Yep, that's what I mean. It would be nice to have the option to sort via Vue, for the models that's appropriate for. Then it would be doable! I don't have a good sense of how much data js can deal with, but I've done it by hand (not Nova) for projects with the amount of data in my use case, and it's a perfect solution.

thiskbj avatar Apr 04 '19 18:04 thiskbj

Why not extend nova resource sorting by the ability to define raw sql order by criteria? Like this:

Boolean::make('Package', fn () => $this->model()->getAttribute('types')['package'])
    ->sortableDb(fn () => "types->'package'"),

You would (as before) define a closure for php on how to render the information and define a database operation which will sort the rows the same like you did with your render function. This string could be simply be used with eloquents orderByRaw operation.

It's the only way it could work and it's a very sane approach.

tpetry avatar Feb 09 '20 11:02 tpetry