Skip to content

Eager chain load ancestors, Model must be node error #403

@liepumartins

Description

@liepumartins

I have products, and posts, that belong to category.

$products = Product::with('category.ancestors')->where('/*some constraints*/')->get()

However this results in Model must be node. thrown InvalidArgumentException …/vendor/kalnoy/nestedset/src/BaseRelation.php40

Reading through #271 I got the impression that this was fixed in #370.

Anyway what I ultimately need is this:
$products = Product::with('category.ancestors.posts')->where(..)->get()

And unfortunately I cannot approach this from other end e.g. Category::with('products')->with('ancestors')->with('posts') because Products have to be selected using different criteria than category.

EDIT: Currently I have to do something resource intensive as:

public function getAllPostsAttribute() {
    $posts = new Collection();
    $posts = $posts->merge($this->posts);
    foreach($this->ancestors as $ancestor) {
        $posts = $posts->merge($ancestor->posts);
    }
    return $posts;
}

This then gets called n times (as many unique categories there are in the products set)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @liepumartins

        Issue actions

          Eager chain load ancestors, Model must be node error · Issue #403 · lazychaser/laravel-nestedset