2017 © Pedro Peláez
 

library nestablecollection

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

image

typicms/nestablecollection

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  • Monday, May 7, 2018
  • by sdebacker@gmail.com
  • Repository
  • 5 Watchers
  • 30 Stars
  • 74,402 Installations
  • PHP
  • 9 Dependents
  • 0 Suggesters
  • 13 Forks
  • 0 Open issues
  • 15 Versions
  • 7 % Grown

The README.md

NestableCollection

Software License, (*1)

A Laravel Package that extends collections to handle nested items following adjacency list model., (*2)

Installation

Run composer require typicms/nestablecollection, (*3)

Usage

The model must have a parent_id attributes :, (*4)

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

and must use the following trait:, (*5)

use TypiCMS\NestableTrait;

Now each time you get a collection of that model, it will be an instance of TypiCMS\NestableCollection in place of Illuminate\Database\Eloquent\Collection., (*6)

If you want a tree of models, simply call the nest method on a collection ordered by parent_id asc :, (*7)

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

Of course you will probably want a position column as well. So you will have to order first by parent_id asc and then by position asc., (*8)

Change the name of subcollections

By default, the name of the subcollections is items, but you can change it by calling the childrenName($name) method : For example if you want your subcollections being named children:, (*9)

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

Indented and flattened list

listsFlattened() method generate the tree as a flattened list with id as keys and title as values, perfect for select/option, for example :, (*10)

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

To use it, first call the nest() method, followed by the listsFlattened() method:, (*11)

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

By default it will look for a title column. You can send a custom column name as first parameter:, (*12)

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

Four spaces are used to indent by default, to use your own use the setIndent() method, followed by the listsFlattened() method:, (*13)

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

Results:, (*14)

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

Nesting a subtree

This package remove items that have missing ancestor, this doesn’t allow you to nest a branch of a tree. To avoid this, you can use the noCleaning() method:, (*15)

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

The Versions

07/05 2018

dev-master

9999999-dev

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

07/05 2018

v1.1.10

1.1.10.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

02/05 2018

v1.1.9

1.1.9.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

31/01 2018

v1.1.8

1.1.8.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

05/06 2017

v1.1.7

1.1.7.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

06/04 2017

v1.1.6

1.1.6.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

05/04 2017

v1.1.5

1.1.5.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

14/01 2017

v1.1.4

1.1.4.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

19/09 2016

v1.1.3

1.1.3.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

07/07 2016

v1.1.2

1.1.2.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

13/05 2015

v1.1.1

1.1.1.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

24/03 2015

v1.1.0

1.1.0.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

03/03 2015

v1.0.2

1.0.2.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

02/03 2015

v1.0.1

1.0.1.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms

18/02 2015

v1.0.0

1.0.0.0

A Laravel Package that extends Collection to handle unlimited nested items following adjacency list model.

  Sources   Download

MIT

The Requires

 

laravel collection nested set eloquent tree adjacency list typicms