Oleg Nurutdinov
Oleg Nurutdinov

Reputation: 633

Problem with composer package on production server - Laravel project

My problem is BadMethodCallException when trait method calls. The package is kalnoy/nestedset for Laravel. On my local server all works perfect, but then I create my app on production server, I always have this exception: Method Illuminate\Database\Query\Builder::descendants does not exist.

What I tried to do without result: 1. composer init 2. composer dump-autoload 3. delete and deployed project few times 4. composer require kalnoy/nestedset 5. Many hours of googling. What thoughts about it? Has anyone had such a problem and sloved it?

My model:

namespace App;

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

class Category extends Model
{
    use NodeTrait;

    protected $table = 'categories';

    public function getOnMainItems($limit = 6)
    {
        return Category::where('main', 1)->limit($limit)->get();
    }
}

Method what "doesn't exist":

namespace App\Services;

use App\Category;
use Kalnoy\Nestedset\Collection;

class CategoryService
{

    /**
     * @param string $slug
     * @return array $categories
     */
    public function getCatalogCategoriesForFilter(string $slug = '')
    {
        $categories = [];
        if ($slug !== '') {
            $currentCategory = Category::where([['category_slug', $slug], ['active', 1]])->limit(1)->get();
            $categoriesByParent = Category::defaultOrder()->descendantsOf($currentCategory[0]->id);
            foreach ($categoriesByParent as $category) {
                $categories[] = $category->id;
            }
        }
        return $categories;
    }
}

My composer.json file

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
    "php": "^7.1.3",
    "darryldecode/cart": "~4.0",
    "doctrine/dbal": "^2.8",
    "fideloper/proxy": "^4.0",
    "kalnoy/nestedset": "^4.3",
    "laravel/framework": "5.6.*",
    "laravel/tinker": "^1.0",
    "predis/predis": "^1.1"
},
"require-dev": {
    "filp/whoops": "^2.0",
    "fzaninotto/faker": "^1.4",
    "mockery/mockery": "^1.0",
    "nunomaduro/collision": "^2.0",
    "phpunit/phpunit": "^7.0"
},
"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
},
"extra": {
    "laravel": {
        "dont-discover": [
        ]
    }
},
"scripts": {
    "post-root-package-install": [
        "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "@php artisan key:generate"
    ],
    "post-autoload-dump": [
        "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
        "@php artisan package:discover"
    ]
},
"config": {
    "preferred-install": "dist",
    "sort-packages": true,
    "optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true

}

Thanks everyone.

UPDATE: Error message:

(1/1) BadMethodCallException
Method Illuminate\Database\Query\Builder::descendants does not exist.

in Builder.php line 2816
at Builder->__call('descendants', array(2))
in Builder.php line 1286
at Builder->__call('descendants', array(2))
in CategoryService.php line 38
at CategoryService->getCatalogCategories('postelnoe-bele')
in GoodsController.php line 41

Upvotes: 0

Views: 495

Answers (1)

Oleg Nurutdinov
Oleg Nurutdinov

Reputation: 633

Thanks everyone who helps me. I sloved this.

The problem was in

Category::where('active', 1)->descendants($categoryId);

The Category::where('active', 1) returns queryBuilder object, which has not descendants method, it availabe on Category (model object).

The right way is

Category::descendants($categoryId);

I was embarrassed cause wrong way works on my local server. If somebody can explain this thing I would very grateful.

Upvotes: 0

Related Questions