uccello/eloquent-tree

Eloquent Tree 将模型转换为树形模型,用于 Laravel Eloquent ORM。

1.2.0 2024-08-25 15:30 UTC

This package is auto-updated.

Last update: 2024-09-25 15:38:39 UTC


README

Eloquent Tree 将模型转换为树形模型,用于 Laravel Eloquent ORM。

本项目基于由 Adrian Skierniewski 创建的原始项目。由于使用了 IsTree 特性,它已被修改以使用此功能,而不是扩展 Tree 模型。如果您希望模型扩展其他类,这将很有用。

目录

##功能

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

安装

对于 Laravel >= 5.3

首先通过 Composer 安装此包。编辑您的项目 composer.json 文件,以要求 uccello/eloquent-tree。

"require": {
    "uccello/eloquent-tree": "1.*"
},
"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

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

鸣谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件