Code Monkey home page Code Monkey logo

nestablecollection's Introduction

NestableCollection

Software License StyleCI

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 :

protected $fillable = [
    '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.

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:

$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 :

[
    '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 can send a custom column name as first parameter:

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:

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

Results:

[
    '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:

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

nestablecollection's People

Contributors

aimalamiri avatar frezno avatar izshreyansh avatar nwidart avatar peckrob avatar sdebacker avatar shankhadevpadam avatar tonyrouse avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.