gzero/eloquent-tree

Eloquent Tree 是 Laravel Eloquent ORM 的树模型。

v3.1.2 2018-09-15 06:00 UTC

This package is auto-updated.

Last update: 2024-09-29 04:08:40 UTC


README

Eloquent Tree 是 Laravel Eloquent ORM 的树模型。

目录

##特性

  • 创建根、子节点和兄弟节点
  • 获取子节点
  • 获取后代
  • 获取祖先
  • 移动子树
  • 在 PHP 侧构建树

安装

版本 1.0 与 0 不兼容。*

版本 2.0 - 支持 Laravel 5

版本 2.1 - 支持 Laravel 5.1

版本 3.0 - 支持 Laravel 5.3

首先,通过 Composer 安装此包。编辑你的项目 composer.json 文件,以 require gzero/eloquent-tree。

"require": {
    "laravel/framework": "5.3.*",
    "gzero/eloquent-tree": "v3.0.*"
},
"minimum-stability" : "stable"

然后,从终端更新 Composer

composer update

现在,你可以在项目中扩展 \Gzero\EloquentTree\Model\Tree

迁移

只需迁移所有必要的列,你可以通过添加新字段进行扩展

Schema::create(
    'trees',
    function (Blueprint $table) {
        $table->increments('id');
        $table->string('path', 255)->nullable();
        $table->integer('parent_id')->unsigned()->nullable();
        $table->integer('level')->default(0);
        $table->timestamps();
        $table->index(array('path', 'parent_id', 'level'));
        $table->foreign('parent_id')->references('id')->on('contents')->onDelete('CASCADE');
    }
);

示例用法

插入和更新新节点

$root       = new Tree(); // New root
$root->setAsRoot();
$child      = with(new Tree())->setChildOf($root); // New child
$sibling    = new Tree();
$sibling->setSiblingOf($child); // New sibling

获取树节点

叶子 - 返回根节点

$leaf->findRoot();

子节点 - 返回平铺的子节点集合。你可以使用 Eloquent 查询构造器。

$collection = $root->children()->get();
$collection2 = $root->children()->where('url', '=', 'slug')->get();

祖先 - 返回平铺的祖先集合,第一个是根节点,最后一个是当前节点。你可以使用 Eloquent 查询构造器。当然,如果你在查询中添加了额外的 where 条件,不能保证树的结构的完整性。

$collection = $node->findAncestors()->get();
$collection2 = $node->findAncestors()->where('url', '=', 'slug')->get();

后代 - 返回平铺的后代集合,第一个是当前节点,最后一个是叶子节点。你可以使用 Eloquent 查询构造器。当然,如果你在查询中添加了额外的 where 条件,不能保证树的结构的完整性。

$collection = $node->findDescendants()->get();
$collection2 = $node->findDescendants()->where('url', '=', 'slug')->get();

在 PHP 侧构建树结构 - 如果某些节点缺失,这些分支将不会构建

$treeRoot = $root->buildTree($root->findDescendants()->get())

获取叶子节点

Tree::getLeaves();

从数组映射

三个新的根节点,第一个带有后代

 Tree::mapArray(
            array(
                array(
                    'children' => array(
                        array(
                            'children' => array(
                                array(
                                    'children' => array(
                                        array(
                                            'children' => array()
                                        ),
                                        array(
                                            'children' => array()
                                        )
                                    )
                                ),
                                array(
                                    'children' => array()
                                )
                            )
                        ),
                        array(
                            'children' => array()
                        )
                    )
                ),
                array(
                    'children' => array()
                ),
                array(
                    'children' => array()
                )
            )
 );

渲染树

你可以通过 buildTree 函数渲染由构建的树

 $html = $root->render(
        'ul',
        function ($node) {
            return '<li>' . $node->title . '{sub-tree}</li>';
        },
        TRUE
        );
 echo $html;

事件

所有树模型都有额外的事件

  • updatingParent
  • updatedParent
  • updatedDescendants

你可以使用它们,例如,更新额外的表

支持

如果你喜欢我的工作,请考虑进行小额捐赠,这样我可以继续维护和创建新的软件来帮助其他用户。

PayPal