• Laravel
  • PHP
  • Laravel Nestedset – Nested Sets pattern implementation

    Nested sets or Nested Set Model is a way to effectively store hierarchical data in a relational table. From wikipedia:

    The nested set model is to number the nodes according to a tree traversal, which visits each node twice, assigning numbers in the order of visiting, and at both visits. This leaves two numbers for each node, which are stored as two attributes. Querying becomes inexpensive: hierarchy membership can be tested by comparing these numbers. Updating requires renumbering and is therefore expensive.

    Applications

    NSM shows good performance when tree is updated rarely. It is tuned to be fast for getting related nodes. It’is ideally suited for building multi-depth menu or categories for shop.

    Documentation

    Suppose that we have a model Category; a $node variable is an instance of that model and the node that we are manipulating. It can be a fresh model or one from database.

    Relationships

    Node has following relationships that are fully functional and can be eagerly loaded:

    • Node belongs to parent
    • Node has many children
    • Node has many ancestors
    • Node has many descendants

    Github: https://github.com/lazychaser/laravel-nestedset

    Để lại một bình luận

    Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

    1 mins