ideas icon indicating copy to clipboard operation
ideas copied to clipboard

Taking the CP order into account when executing a querybuilder get(), for structured collections

Open Jars-of-jam-Scheduler opened this issue 4 years ago • 1 comments

Hi all,

I am executing the following query:

$this->getAugmentedValue(Collection::find('skills')->queryEntries()->where('status', 'published')->get())

In order to get the entries of the collection "skills". in fact, "skills" is a structured collection; i.e.: there is a tree file located under content/trees/collections/ directory that is associated to this collection. This tree by definition contains the collection's entries in the order defined by the drag'n drops the user performed in control panel in the tree listing of the entries.

However, the above line of code doesn't take into account this drag'n drop order. My question is: could you please add a feature to take it into account? You could for example add a SQL numeric field "cpDragNDropOrder" , allowing us to do a orderBy('cpDragNDropOrder').

Thank you in advance. Best regards,

Jars-of-jam-Scheduler avatar Sep 09 '21 12:09 Jars-of-jam-Scheduler

This is certainly not the best way to do it (I presume anyway, maybe someone else can confirm) but this should do the job if you need a workaround.

collect(\Statamic\Facades\Collection::find('skills')->structure()->in('default')->tree())
    ->map(function ($item) {
        return \Statamic\Facades\Entry::find($item['entry']);
    })
    ->filter(function ($entry) {
        return $entry->published();
    });

duncanmcclean avatar Sep 09 '21 12:09 duncanmcclean