Support the ongoing development of Laravel.io →
Database Packages Architecture

Hi, We're using the lazychaser/laravel-nestedset (on gidhub) / Kalnoy\Nestedset (composer/packagist) package. The seeder is working. But when i want to get the tree by a function in my model, it gives errors that are untraceable for me:

(deleted parts not needed for problem solving)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Kalnoy\Nestedset\NodeTrait;


/**
 * Class Category
 */
class Category extends Model
{
    use \Dimsav\Translatable\Translatable;
    use NodeTrait;

    protected   $fillable = [
        'visible'
    ];

    /**
     * Get the product records associated with the category.
     */
    public function products()
    {
        return $this->hasMany('App\Product');
    }
   
    /**
     * Get the category feature records associated with the category.
     */
    public function categoryFeatures()
    {
        return $this->hasMany('App\CategoryFeature'); // this one is working right!
    }

    /**
     * Get the category feature records associated with the category.
     */
    public function allCategoryFeatures()
    {
        // when visiting the url where i'm 3 levels deep, i'm getting the right category, but as you see:
        print_r($this->isRoot());  // returns: 1       
        print_r($this->ancestors()->get()->toTree()); // returns: Kalnoy\Nestedset\Collection Object ( [items:protected] => Array ( ) )
        // it doesn't seem to recognise the nestedset system

        $ancestors         = ($this->isRoot()) ? NULL : $this->ancestors()->get();

        $categoryCollection   = (empty($ancestors)) ? $this->with('categoryFeatures') : $ancestors->toTree()->with('categoryFeatures');

        return $categoryCollection;
    }
///////////
// etc
//////////
}

As you can see in the comments in the code it doesn't seem to get the nestedset system...

In the end i'm getting:

BadMethodCallException in Builder.php line 2437:
Call to undefined method Illuminate\Database\Query\Builder::addEagerConstraints()
1. in Builder.php line 2437
2. at Builder->__call('addEagerConstraints', array(array(object(Category))))
3. at call_user_func_array(array(object(Builder), 'addEagerConstraints'), array(array(object(Category)))) in Builder.php line 1433
4. at Builder->__call('addEagerConstraints', array(array(object(Category)))) in Builder.php line 655
5. at Builder->loadRelation(array(object(Category)), 'allCategoryFeatures', object(Closure)) in Builder.php line 633
6. at Builder->eagerLoadRelations(array(object(Category))) in Builder.php line 324
........................
etc
........................

can't $this be accessed in the model or something? do you have any ideas on how to get this working?

Bart

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

BartHuis barthuis Joined 19 May 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.