1. Go to this page and download the library: Download vicklr/materialized-model 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/ */
vicklr / materialized-model example snippets
use Vicklr\MaterializedModel\Traits\HasMaterializedPaths;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasMaterializedPaths;
}
use Vicklr\MaterializedModel\MaterializedModel;
class Dictionary extends MaterializedModel
{
protected $table = 'dictionary';
// 'parent_id' column name
protected string $parentColumn = 'parent_id';
// 'depth' column name
protected string $depthColumn = 'depth';
// 'path' column name
protected string $pathColumn = 'path';
// 'order' column name
protected string $orderColumn = 'weight';
// guard attributes from mass-assignment
protected $guarded = array('id', 'parent_id', 'depth', 'path', 'weight');
}
class CreateCategoriesTable extends Migration {
public function up() {
Schema::create('categories', function(Blueprint $table) {
$table->id();
$table->materializedFields(parent_name: 'parent_id', path_name: 'path', depth_name: 'depth', primary_name: 'id');
$table->materializedOrdering(order_name: 'weight');
});
}
public function down() {
Schema::drop('categories');
}
}
// This will work without depth limiting
// 1. As usual
$node->getDescendants();
// 2. Selecting only some attributes
$other->getDescendants(array('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, array('id', 'parent_id', 'name'));
$nodes = ... // Collection of nodes retrieved from the database, by ids or some other means
$ancestors = (new HierarchyCollection($nodes))->setClassName(Category::class)->getAncestors();
// $ancestors will now contain all ancestors of all the nodes in the collection
Category::roots()->each->rebuild();
$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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.