Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/TypiCMS/NestableCollection
A Laravel Package that extends Eloquent\Collection to handle nested items following adjacency list model.
https://github.com/TypiCMS/NestableCollection
Last synced: 3 months ago
JSON representation
A Laravel Package that extends Eloquent\Collection to handle nested items following adjacency list model.
- Host: GitHub
- URL: https://github.com/TypiCMS/NestableCollection
- Owner: TypiCMS
- License: mit
- Created: 2015-02-18T15:37:50.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-05-17T10:13:49.000Z (8 months ago)
- Last Synced: 2024-05-17T11:29:02.257Z (8 months ago)
- Language: PHP
- Homepage:
- Size: 51.8 KB
- Stars: 86
- Watchers: 9
- Forks: 29
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
- awesome - TypiCMS/NestableCollection - A Laravel Package that extends Eloquent\Collection to handle nested items following adjacency list model. (PHP)
README
# NestableCollection
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
A Laravel Package that extends collections to handle nested items following adjacency list model.
## Installation
Run `composer require typicms/nestablecollection`
## Usage
The model must have a **parent_id** attributes :
```php
protected $fillable = [
'parent_id',
// …
];
```and must use the following trait:
```php
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 :
```php
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.
## 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**:```php
$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 :
```php
[
'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:
```php
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:
```php
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:
```php
Model::orderBy('parent_id')->get()->nest()->setIndent('> ')->listsFlattened();
```Results:
```php
[
'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:```php
Model::orderBy('parent_id')->get()->noCleaning()->nest();
```