statamic / eloquent-driver

Provides support for storing your Statamic data in a database, rather than flat files.
https://statamic.dev/tips/storing-content-in-a-database
MIT License
104 stars 73 forks source link

Unable to sort on computed fields #149

Closed robdekort closed 1 year ago

robdekort commented 1 year ago

When you have computed fields you can't sort listing tables on them. The fields are calculated at run time and not stored in the database. Therefore isn't available for the database to sort on.

I use this

Collection::computed('applications', 'outing_title', function ($entry) {
    return optional($entry->outing)->title;
});

To make sure I can properly sort on an entries field, but alas.

ryanmitchell commented 1 year ago

We should store the computed field values in the database too. I'll work on that.

robdekort commented 1 year ago

You are awesome!

jackmcdade commented 1 year ago

He sure is! ❤️

AntonCooper commented 1 year ago

Would storing these values not become problematic? I've got many implementations of computed fields which require a dynamic values - I thought that was the whole purpose of computed fields, that they're have the ability to be evaluated at runtime?

ryanmitchell commented 1 year ago

The values would be stored so the value at the point of last save could be queried (sorting etc), but when retrieved from the data would be overwritten with your dynamic values. It should cover both use cases.