PHP code example of typicms / nestablecollection

1. Go to this page and download the library: Download typicms/nestablecollection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

typicms / nestablecollection example snippets


protected $fillable = [
    'parent_id',
    // …
];

use TypiCMS\NestableTrait;

class Category extends Model
{
    use NestableTrait;
}

Category::orderBy('parent_id')->get()->nest();

Category::orderBy('parent_id')->orderBy('position')->get()->nest();

$collection->parentColumn('category_id')->nest();

$collection->childrenName('children')->nest();

[
    22 => 'Item 1 Title',
    10 => '    Child 1 Title',
    17 => '    Child 2 Title',
    14 => 'Item 2 Title',
]

Category::orderBy('parent_id')->get()->nest()->listsFlattened();

$collection->nest()->listsFlattened('name');

$collection->nest()->setIndent('> ')->listsFlattened();

[
    22 => 'Item 1 Title',
    10 => '> Child 1 Title',
    17 => '> Child 2 Title',
    14 => 'Item 2 Title',
]

$collection->nest()->setIndent(' / ')->listsFlattenedQualified();

[
    22 => 'Item 1 Title',
    10 => 'Item 1 Title / Child 1 Title',
    17 => 'Item 1 Title / Child 2 Title',
    14 => 'Item 2 Title',
]

$nested = Category::orderBy('parent_id')->get()->nest()->setParents();

$nested[0]->items[0]->parent; // Returns the parent model

Category::orderBy('parent_id')->get()->noCleaning()->nest();