Skip to content

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

Open
@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)

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

    Issue actions