1. Go to this page and download the library: Download encima-io/albero 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/ */
encima-io / albero example snippets
namespace Encima\Albero\Models\Category;
use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasNestedSets;
}
use Encima\Albero\HasNestedSets;
use Encima\Albero\Models\Category;
use Illuminate\Database\Eloquent\SoftDeletes;
class SoftCategory extends Category
{
use SoftDeletes, HasNestedSets;
public static function boot() {
parent::boot();
static::restoring(function ($node) {
$node->shiftSiblingsForRestore();
});
static::restored(function ($node) {
$node->restoreDescendants();
});
}
}
use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasNestedSets;
}
use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasNestedSets;
protected $table = 'categories';
// 'parent_id' column name
protected $parentColumn = 'parent_column';
// 'left' column name
protected $leftColumn = 'left_column';
// 'right' column name
protected $rightColumn = 'right_column';
// 'depth' column name
protected $depthColumn = 'depth_column';
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('parent_id')->nullable();
$table->unsignedInteger('left')->nullable();
$table->unsignedInteger('right')->nullable();
$table->unsignedInteger('depth')->nullable();
$table->string('name');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('categories');
}
}
// This will work without depth limiting
// 1. As usual
$node->getDescendants();
// 2. Selecting only some attributes
$other->getDescendants(['id', 'parent_id', 'name']);
...
// With depth limiting
// 1. A maximum of 5 levels of children will be returned
$node->getDescendants(5);
// 2. A max. of 5 levels of children will be returned selecting only some attrs
$other->getDescendants(5, ['id', 'parent_id', 'name']);
use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasNestedSets;
public static function boot() {
parent::boot();
static::moving(function($node) {
// Before moving the node this function will be called.
});
static::moved(function($node) {
// After the move operation is processed this function will be called.
});
}
}
use Encima\Albero\HasNestedSets;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasNestedSets;
protected $scoped = ['company_id'];
}
$node = Category::where('name', '=', 'Some category I do not want to see.')->first();
$root = Category::where('name', '=', 'Old boooks')->first();
var_dump($root->descendantsAndSelf()->withoutNode($node)->get());
... // <- This result set will not contain $node
public static function getNestedList($column, $key = null, $seperator = ' ');