Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
liepumartins opened this issue Sep 9, 2019 · 0 comments
Open

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

liepumartins opened this issue Sep 9, 2019 · 0 comments

Comments

@liepumartins
Copy link

liepumartins commented Sep 9, 2019

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant