paxha/laravel-recursive-relationships 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

paxha/laravel-recursive-relationships

最新稳定版本:v1.1.0

Composer 安装命令:

composer require paxha/laravel-recursive-relationships

包简介

This Laravel Eloquent extension provides recursive relationships using common table.

README 文档

README

Build Status StyleCI Total Downloads Latest Stable Version License

Introduction

This Laravel Eloquent extension provides recursive relationships using common table.

Installation

composer require paxha/laravel-recursive-relationships

Usage

Getting Started

Consider the following table schema for hierarchical data:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('parent_id')->nullable();
});

Use the HasRecursiveRelationships trait in your model to work with recursive relationships:

class User extends Model
{
    use \RecursiveRelationships\Traits\HasRecursiveRelationships;
}

By default, the trait expects a parent key named parent_id. You can customize it by overriding getParentKeyName():

class User extends Model
{
    use \RecursiveRelationships\Traits\HasRecursiveRelationships;

    public function getParentKeyName()
    {
        return 'user_id'; // or anything
    }
}

Relationships

The trait provides various relationships:

  • children(): The model's direct children.
  • nestedChildren(): The model's nested children.
  • parent(): The model's direct parent.
  • nestedParents(): The model's nested parents by object.
$users = User::with('children')->get();

$users = User::with('nestedChildren')->get();

$users = User::with('parent')->get();

$users = User::with('nestedParents')->get();

Scopes

The trait provides query scopes to filter models by their position in the tree:

  • hasChildren(): Models with children.
  • hasParent(): Models with a parent.
  • leaf(): Models without children.
  • root(): Models without a parent.
$noLeaves = User::hasChildren()->get();

$noRoots = User::hasParent()->get();

$leaves = User::leaf()->get();

$roots = User::root()->get();

Functions

The trait provides helper functions:

  • descendents(): The model's all Children in single array.
  • ancestors(): The model's all parents in single array.
  • siblings(): The parent's other children.
$descendents = User::find($id)->descendents();

$ancestors = User::find($id)->ancestors();

$siblings = User::find($id)->siblings();

License

This is open-sourced laravel library licensed under the MIT license.

统计信息

  • 总下载量: 50.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-11