atayahmet / laravel-nestable

Laravel 5 nested category/menu generator
MIT License
214 stars 52 forks source link

I cannot use Eloquent methods #5

Closed ZedanLab closed 8 years ago

ZedanLab commented 8 years ago

Hello @atayahmet I've just installed your package to handle categories, but when I use Nestable\NestableTrait all Eloquent methods are not working as where(), findOrFail(), delete() .... And if I remove NestableTrait everything working good.

Any solutions please ..

ZedanLab commented 8 years ago

e.g.

ZedanLab commented 8 years ago

I think the __call method in NestableTrait that are causing the problem.

please help me @atayahmet

atayahmet commented 8 years ago

Hi @Mohamed-Zedan

i fixed the problem. Please update the package and try again. Check the commits: aae8fe5 96be14f45

ZedanLab commented 8 years ago

Hello again @atayahmet It has been fixed. But I got new problem with relations methods, and again __call method causing the problem.

Category model: ` <?php

namespace Modules\Arinashop\Entities;

use Cviebrock\EloquentSluggable\Sluggable; use Cviebrock\EloquentSluggable\SluggableScopeHelpers; use Illuminate\Database\Eloquent\Model; use Nestable\NestableTrait;

class Category extends Model { use Sluggable; use SluggableScopeHelpers; use NestableTrait;

protected $parent = 'parent_id';

/**
 * Return the sluggable configuration array for this model.
 *
 * @return array
 */
public function sluggable() {
    return [
        'slug' => [
            'source' => ['name', 'parentName.name'],
        ],
    ];
}

protected $table    = 'categories';
protected $fillable = ['name', 'name_ar', 'description', 'description_ar', 'image', 'parent_id'];

public function parentName() {
    return $this->belongsTo('Modules\Arinashop\Entities\Category', 'parent_id');
}

public function children() {
    return $this->hasMany('Modules\Arinashop\Entities\Category', 'parent_id');
}

public function products() {
    return $this->hasMany('Modules\Arinashop\Entities\Product', 'category_id', 'id');
}

public function tags() {
    $products = $this->hasMany('Modules\Arinashop\Entities\Product', 'category_id', 'id')->get();
    $tags     = array();

    foreach ($products as $product) {
        foreach ($product->tags as $tag) {
            $tags[] = $tag;
        }
    }

    return $tags;
}

}

`

Problem is when I call any relations methods such as products() or children() after update package, these methods return all items from all categories. And again when I comment __call function it's works.

atayahmet commented 8 years ago

Hello @muhammed

This package is not designed with use relations. You can use helper or macro way as alternative without NestableTrait.

Please check the docs.

ZedanLab commented 8 years ago

@atayahmet Ok I got it.. Thanks alot my friend :+1: