Nesting

You may wish for a Crud to have a nested structure, such as categories and their subcategories. A system was added to Enso in 3.0 to allow for this and was used to create the Categories system.

Nesting is made possible using the lazychaser/laravel-nestedset, so if you want to use custom column names or other customisations you can read the documentation there.

Warning!

Nesting is fairly complicated. It's quite easy to set up but you should consider what will happen when items in your tree are deleted. By default lazychaser/laravel-nestedset deletes all child nodes. **You should not delete items any other way, e.g. Category::where('id', '=', $id)->delete(); as this will break the tree. For more info, read the lazychaser/laravel-nestedset documentation.

Adding nesting

  1. Add parent_id, _lft and _rgt integer columns to your model's table.

    $table->nestedSet();
    
  2. Apply the Kalnoy\Nestedset\NodeTrait and Yadda\Enso\Crud\Traits\IsCrudModel traits to your model.

  3. Tell your Crud config to treat the data as nested:

<?php

namespace App\Crud;

class Category extends Config
{
    public function configure()
    {
        // ...

        $this
          ->nested()
          ->order('_lft', 'ASC');
    }
}
  1. You may also want to extend Yadda\Enso\Categories\Controllers\CategoryController to provide JSON routes to access your model.
<?php

namespace \App\Http\Controllers;

use Yadda\Enso\Categories\Controllers\TaxonomyController;
use App\MyTaxonomy;

class CategoryController extends TaxonomyController
{
    public function modelClass()
    {
        return MyTaxonomy::class;
    }

    public function taxonomyName()
    {
        return 'my-taxonomy';
    }
}

You may also wish to override the toArray method on your controller to alter what is returned.

  1. Give your Model a custom collection class
use \Kalnoy\Nestedset\Collection;

/**
  * Create a new Eloquent Collection instance.
  *
  * @param  array  $models
  * @return Collection
  */
public function newCollection(array $models = [])
{
    return new Collection($models);
}
  1. If you have existing data, you can now fix the tree structure. This will use the parent_id column to populate the _lft and _rgt columns.
MyTaxonomy::fixTree();

Max Depth

You can limit how many levels of nesting are available. The default is -1, meaning no limit.

To be clear, a depth of two means two levels (e.g. parents and children), not two levels of descendants (e.g. children and grandchildren).

<?php

namespace App\Crud;

class Category extends Config
{
    public function configure()
    {
        // ...

        $this
          ->nested()
          ->maxDepth(2);
    }
}

Note: This will only affect your ability to drag and drop the cruds in the admin area. It will not prevent you from nesting deeper in your own code. Nor will it fix any items that may already exist at a deeper depth - they just won't be visible when rearranging them in the admin area.

Preserving Children

When an item is deleted (which should only be done by calling the delete() method on a Model), descendant items will also be removed. To avoid this you can apply the Yadda\Enso\Crud\Traits\PreservesChildren trait to your model. The will move child items up a level, replacing the position of the parent item that is deleted. The child item's order and descendants will be preserved.