atayahmet / laravel-nestable

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

$categories = Category::nested()->get(); #39

Closed ghost closed 6 years ago

ghost commented 6 years ago

Im using laravel 5.5. I have added the package. I have also added the trait to the model,

Where do i need to specify this $categories = Category::nested()->get(); When i do {{$categories = Category::nested()->get();}} it does not show anything on the view

atayahmet commented 6 years ago

Hi @shrikanth003

->nested() method will return array tree. If you want use in html template, you have to choose a render method.

Example:

{{ Category::renderAsHtml() }}

or:

{{ Category::renderAsDropdown() }}

Please check docs: Outputs

ghost commented 6 years ago

Ok. Is theere any tutorial on this? I mean on how to create a nested categories list with Dependent dropdown

atayahmet commented 6 years ago

I think this section will be helpfuly: Render as dropdown

ghost commented 6 years ago

My Categories Model Looks Like this

namespace App\Models; use Illuminate\Database\Eloquent\Model; use Nestable\NestableTrait;

class Category extends Model { use NestableTrait; protected $parent = 'parent_id'; }

My Categories Table looks like id, parent_id, name, slug

I have placed {{Category::renderAsHtml()}} on my Website index page.

My Controller looks like this

namespace App\Http\Controllers;

use Illuminate\Http\Request; use App\Models\Category;

class PagesController extends Controller { /**

I'm a learner and didn't find any proper tutorial on this. Could you correct me or help me understanding how this works. @atayahmet

ghost commented 6 years ago

Because i get a error Class 'Category' not found (View: D:\xampp\htdocs\myproject\resources\views\index.blade.php)

atayahmet commented 6 years ago

Ok.The default path for models is the app directory

You can call this way:

{{ \App\Category::renderAsHtml() }}

But if you want to call with direct model name, run the command below:

Terminal:

$ composer dump-autoload

Now you can use this way:

{{ Category::renderAsHtml() }}
ghost commented 6 years ago

It worked! Tysm :)

zymawy commented 6 years ago

This Is Strange The Are No Results When DD(\App\Category::nested()->get()) When Rendering The Category::all(); Indeed It Worked I Tryied $ composer dump-autoload