zerochip/laravel-lineage 问题修复 & 功能扩展

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

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

zerochip/laravel-lineage

Composer 安装命令:

composer require zerochip/laravel-lineage

包简介

A Laravel package for chain calling eloquent relationships

README 文档

README

A package for chain calling Laravel relationships. Some relationships return collections so can't be chained. This package provides a way to get past that.

Installation

Install with composer

composer require zerochip/laravel-lineage

Add the ServiceProvider to your providers array in config/app.php

'providers' => [
    // Other Service Providers

    Zerochip\LineageServiceProvider::class,
];

Quickstart

Add the lineage trait to your model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Zerochip\Lineage\LineageTrait

class Subject extends Model
{
    use LineageTrait;
}

For example to get a quizzes for a subject.

Without lineage we would have had to do this:

$subject = Subject::find(1);
$quizzes = [];

foreach ($subject->courses as $course) {

    foreach ($course->chapters as $chapter) {

        foreach($chapter->topics as $topic) {

            foreach($topic->sections as $section) {
               
                $quizzes = $quizzes->merge($section->quizzes);
            }
        }
    }
}

Using Laravel-Lineage we just do the following

$subject = Subject::find(1);
$quizzes = $subject->lineage('courses->chapters->topics->sections->quizzes');

You can also get the lineage for a collection.

$subjects = Subject::get();
$quizzes = $subjects->lineage('courses->chapters->topics->sections->quizzes');

We can even get the inverse of that

$quiz = Quiz::find(1);
$subject = $quiz->lineage('section->topic->chapter->course->subject')->first();

NB: lineage always returns a collection hence the use of first in the above

Lineage format

Lineage format rules are as follows:

  1. Uses the delimiter ->.

  2. Should be a string

    $subject->lineage('course->chapters');
    
    // the following won't work
    $subject->lineage(course->chapters);
  3. Should have at least 2 levels

    $subject->lineage('course->chapters');
    
    // the following will cause an error
    $subject->lineage('course');
  4. If preferred can start with ->

    $subject->lineage('course->chapters');
    
    // produces the same result as above
    $subject->lineage('->course->chapters');
  5. Can not be open ended

    $subject->lineage('course->chapters');
    
    // the following will cause an error
    $subject->lineage('course->chapters->');

Usage without trait and collection macro

The lineage class can be used directly. This class has one static method get() which takes the object (model or collection) as the first parameter and the lineage as the second

use Zerochip\Lineage;

$subject = Subject::find(1);
$topics = Lineage::get($subject, 'course->chapters->topics');

// The same with a collection
$courses = Courses::get();
$sections = Lineage::get($courses, 'chapters->topics->sections');

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固