PHP code example of zerochip / laravel-lineage

1. Go to this page and download the library: Download zerochip/laravel-lineage library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zerochip / laravel-lineage example snippets


'providers' => [
    // Other Service Providers

    Zerochip\LineageServiceProvider::class,
];



namespace App;

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

class Subject extends Model
{
    use LineageTrait;
}

$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);
            }
        }
    }
}

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

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

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


    $subject->lineage('course->chapters');
    
    // the following won't work
    $subject->lineage(course->chapters);
    

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

    $subject->lineage('course->chapters');
    
    // produces the same result as above
    $subject->lineage('->course->chapters');
    

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

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');