PHP code example of paxha / laravel-recursive-relationships

1. Go to this page and download the library: Download paxha/laravel-recursive-relationships 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/ */

    

paxha / laravel-recursive-relationships example snippets


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

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

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

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

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

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

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

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

$noLeaves = User::hasChildren()->get();

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

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

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

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

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

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