cms icon indicating copy to clipboard operation
cms copied to clipboard

Querying entries where parent is null (root entries in a structure) does not work

Open MariusSpring opened this issue 3 years ago • 1 comments

Bug description

Hi, I am trying to query all entries that do not have a parent (root entries), however, this does not seem to work.

$entries = Entry::query()
    ->where('collection', 'destinations')
    ->where('parent', null)
    ->get();
$entries = Entry::query()
    ->where('collection', 'destinations')
    ->whereNull('parent')
    ->get();

Logs

No response

Versions

Statamic 3.3.17 Pro Laravel 8.83.18 PHP 8.1.4 doublethreedigital/duplicator 2.3.1 doublethreedigital/runway 2.4.0 spatie/statamic-health 1.0.0 statamic/collaboration 0.4.0

Installation

Existing Laravel app

Antlers Parser

runtime (new)

Additional details

No response

MariusSpring avatar Jul 03 '22 00:07 MariusSpring

The parent data has been abstracted away from being stored on Entries and moved to the Tree structures.

So something like the following should work:

$pages = Collection::findByHandle('destinations')
    ->structure()
    ->in('default')
    ->flattenedPages()
    ->filter
    ->parent('parent-id')

Each element in $pages should be a Statamic\Structures\Page and you can call entry() on it to get the collection entry.

ryanmitchell avatar Jul 04 '22 06:07 ryanmitchell

Can this issue be closed now? I see @ryanmitchell has provided a solution.

duncanmcclean avatar Dec 17 '22 09:12 duncanmcclean