PHPackages                             ohhink/relation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ohhink/relation

ActiveLib[Utility &amp; Helpers](/categories/utility)

ohhink/relation
===============

User Hierarchical Relationship

1.0.2(4y ago)06MITPHP

Since Oct 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ouhaohan8023/relations)[ Packagist](https://packagist.org/packages/ohhink/relation)[ RSS](/packages/ohhink-relation/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (4)Used By (0)

Laravel 层级关系扩展包
===============

[](#laravel-层级关系扩展包)

[![standard-readme compliant](https://camo.githubusercontent.com/4d148b38f9c13f71b15b80f0bd583698fd5a5ab9bd7dfe61d40e1e30aec35399/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f726561646d652532307374796c652d7374616e646172642d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/RichardLitt/standard-readme)

**[English](https://github.com/ouhaohan8023/relations/blob/main/README.md)****[中文](https://github.com/ouhaohan8023/relations/blob/main/README.cn.md)**

> 快速创建层级架构，可用于上下级明确，多层级的任意应用

本拓展包运行基础环境：

1. Php &gt;= 8.0
2. Laravel Version &gt;= 8.6

内容列表
----

[](#内容列表)

- [背景](#%E8%83%8C%E6%99%AF)
- [安装](#%E5%AE%89%E8%A3%85)
- [使用说明](#%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E)
- [相关仓库](#%E7%9B%B8%E5%85%B3%E4%BB%93%E5%BA%93)
- [维护者](#%E7%BB%B4%E6%8A%A4%E8%80%85)
- [如何贡献](#%E5%A6%82%E4%BD%95%E8%B4%A1%E7%8C%AE)
- [使用许可](#%E4%BD%BF%E7%94%A8%E8%AE%B8%E5%8F%AF)

背景
--

[](#背景)

在实际开发中，经常会碰到多层级的组织架构。例如：

```
 A 项目 -> B 项目 -> C 项目 -> D 项目 ... -> Z 项目

 表示：Z项目属于A项目的N级子项目
```

当Z向上找A，或者A向下找Z的时候，如果处理不当，任务的时间复杂度就会变成O(N)。

此拓展包可将上述情况的时间复杂度变为O(1)，并提供相应接口，实现隐式调用。

安装
--

[](#安装)

在项目根目录运行 [composer](https://getcomposer.org/)

```
$ composer require ohhink/relation
```

根目录下运行资源发布，此命令会增加配置文件(`relationship.php`)

```
$ php artisan vendor:publish
```

根目录下运行数据库迁移填充命令

```
# run autoload first
$ composer dump-autoload
$ php artisan migrate
```

向 User 模型添加 `HasRelationship` Trait

```
