Package Data | |
---|---|
Maintainer Username: | webfactorybulgaria |
Maintainer Contact: | sdebacker@gmail.com (Samuel De Backer) |
Package Create Date: | 2015-12-11 |
Package Last Update: | 2015-12-11 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:17:24 |
Package Statistics | |
---|---|
Total Downloads: | 508 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
A Laravel/Lumen Package that extends Collection to handle unlimited nested items following adjacency list model.
Run composer require typicms/nestablecollection
The model must have a parent_id attributes :
protected $fillable = array(
'parent_id',
// …
}
and must use the following trait:
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.
If you want a tree of models, simply call the nest method on a collection ordered by parent_id asc :
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.
listsFlattened()
method generate the tree as a flattened list with id as keys and title as values, perfect for select/option, for example :
[
'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:
Model::orderBy('parent_id')->get()->nest()->listsFlattened();
By default it will look for a title
column. You send as first parameter a custom column name:
Model::orderBy('parent_id')->get()->nest()->listsFlattened('name');