1. Go to this page and download the library: Download efureev/laravel-trees 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/ */
efureev / laravel-trees example snippets
namespace App\Models;
use Fureev\Trees\NestedSetTrait;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use NestedSetTrait;
}
namespace App\Models;
use Fureev\Trees\{NestedSetTrait,Contracts\TreeConfigurable};
use Fureev\Trees\Config\Base;
use Illuminate\Database\Eloquent\Model;
class Category extends Model implements TreeConfigurable
{
use NestedSetTrait;
protected static function buildTreeConfig(): Base
{
return new Base();
}
}
protected static function buildTreeConfig(): Base
{
return Base::make()
->setAttribute('parent', ParentAttribute::make()->setName('papa_id'))
->setAttribute('left', LeftAttribute::make()->setName('left_offset'))
->setAttribute('right', RightAttribute::make()->setName('right_offset'))
->setAttribute('level', LevelAttribute::make()->setName('deeeeep'));
}
namespace App\Models;
// use Fureev\Trees\Config\TreeAttribute;
use Fureev\Trees\Contracts\TreeConfigurable;
use Fureev\Trees\NestedSetTrait;
use Fureev\Trees\Config\Base;
use Illuminate\Database\Eloquent\Model;
class Item extends Model implements TreeConfigurable
{
use NestedSetTrait;
protected $keyType = 'uuid';
protected static function buildTreeConfig(): Base
{
$config= new Base(true);
// $config->parent()->setType('uuid'); <-- `parent type` set up automatically from `$model->keyType`
return $config;
}
/*
or:
protected static function buildTreeConfig(): Base
{
return Base(TreeAttribute::make('uuid')->setAutoGenerate(false));
}
or:
protected static function buildTreeConfig(): Base
{
return Base::make()
->setAttributeTree(TreeAttribute::make()->setName('big_tree_id'))
->setAttribute('parent', ParentAttribute::make()->setName('pid'))
->setAttribute('left', LeftAttribute::make()->setName('left_offset'))
->setAttribute('right', RightAttribute::make()->setName('right_offset'))
->setAttribute('level', LevelAttribute::make()->setName('deeeeep'));
}
*/
}
use Fureev\Trees\Migrate;
use Illuminate\Database\Migrations\Migration;
class AddTemplates extends Migration
{
public function up()
{
Schema::create('trees', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('title');
Migrate::columns($table, (new Page)->getTreeConfig());
$table->timestamps();
$table->softDeletes();
});
}
}
// For single-root tree
Category::make($attributes)->makeRoot()->save();
Category::make($attributes)->saveAsRoot();
Category::create(['setRoot'=>true,...]);
// For multi-root tree. If parent is absent, node set as root.
Page::make($attributes)->save();
// Get all siblings of the node
$collection = $node->siblings()->get();
// Get siblings which are before the node
$collection = $node->prevSiblings()->get();
// Get siblings which are after the node
$collection = $node->nextSiblings()->get();
// Get a sibling that is immediately before the node
$prevNode = $node->prevSibling()->first();
// Get a sibling that is immediately after the node
$nextNode = $node->nextSibling()->first();