atayahmet / laravel-nestable

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

use where in output query #1

Closed mostafaznv closed 8 years ago

mostafaznv commented 8 years ago

hi. thanks for your nice package.

i created a custom database like this:

id parent_id name slug post_type img
1 0 cat 1 cat-1 posts thumb.jpg
2 1 cat 2 cat-2 posts p.png
3 0 cat 3 cat-3 products pthumb.jpg

i want to show only categories for a specific post_type can u help me?

mostafaznv commented 8 years ago

i can override get() in NestableTrait.php like this:

$this->source = parent::where('post_type','posts')->get();

but if i update the package, all these will gone... @atayahmet

mostafaznv commented 8 years ago

i created NestableTraitPlus now i use my own functions and i think it's a safe way ...

atayahmet commented 8 years ago

Hi @Mostafa,

I have seen yet the message.

You can already use it.

Category::nested()->where('post_type', 'posts')->get();

What's the problem exactly?

atayahmet commented 8 years ago

Ohh sorry, I got it now. The problem is output methods. Ok. I will fix it as posibble.

mostafaznv commented 8 years ago

thank you dear ahmet... so, is it safe way to create "NestableTraitPlus" and using new get() function?

or if i should wait for new version, how long does it take?

atayahmet commented 8 years ago

Hi @mostafaznv

it's fixed: 96cac5a You should update the package.

mostafaznv commented 8 years ago

thanks ... it works fine only for nested() but i get these errors for other methods (renderAsDropdown, renderAsHtml): Call to a member function where() on string Call to a member function get() on string

edit: i found the problem i changed this:

public static function renderAsHtml()
{
    return self::nested(static::$toArray)->get();
}

to:

public static function renderAsHtml()
{
    return self::nested(static::$toArray);
}

and when i use $categories = category::renderAsHtml()->where('post_type', 'products')->get(); it works fine

atayahmet commented 8 years ago

Hi @mostafaznv

Please use such: Category::where('post_type', 'posts')->renderAsHtml(); or Category::where('post_type', 'posts')->renderAsDropdown();

Not use such: Category::where('post_type', 'posts')->renderAsDropdown()->get();

mostafaznv commented 8 years ago

but it return all records in my table $categories = category::where('post_type', 'posts')->renderAsHtml();

atayahmet commented 8 years ago

I tested the changes and works fine. Share results please.

mostafaznv commented 8 years ago

this is my table: and with this code:

$categories = \App\category::where('post_type', 'posts')->renderAsHtml();
return $categories;

i get below results:

all of records are shown in output...

atayahmet commented 8 years ago

Hi @mostafaznv

update and try again.

mostafaznv commented 8 years ago

wow ... thanks dear ahmet ... it works fine ...

atayahmet commented 8 years ago

Thank you too for contribution @mostafaznv .