PHP code example of theoluirard / tree-structured-relation

1. Go to this page and download the library: Download theoluirard/tree-structured-relation 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/ */

    

theoluirard / tree-structured-relation example snippets





namespace App\Models;

use theoLuirard\laravelGetTableName\Traits\HasTreeStructure;;
use Illuminate\Database\Eloquent\Model;

class Domaine extends Model
{
    use HasTreeStructure;
}


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->unsignedBigInteger('parent_id')->nullable();
            $table->string('path');
            $table->string('explicit_path'); // Optionnal
            $table->timestamps();

            $table->foreign('parent_id')->references('id')->on('categories')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}




namespace App\Models;

use App\Models\Traits\HasTreeStructure;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasTreeStructure;

    /**
     * The parent column name 
     * 
     * @var string
     */
    public string $parent_column_name = 'parent_id';

    /**
     * The path column name 
     * 
     * @var string
     */
    public string $path_column_name = 'path';

    /**
     * The separator used in path 
     * 
     * @var string 
     */
    public string $path_separator = '/';

    /**
     * The property name used in explicit path 
     * 
     * @var string 
     */
    public string $property_for_explicit_path = 'name';

    /**
     * The explicit_path column name, if this column is not in the table, it will be computed
     */
    public ?string $explicit_path_column_name = null;

}


$category->direct_children

$category->parent

$category->parents

$category->children

$category->siblings


// To filter
Category::whereHas("direct_children.name", "B1")->get();

// To eager load 
Category::with("parent")->get();

// Aggregate function 
Category::withCount('children')->get();


$category->isRoot(); // True if it doesn't have parent

$category->isLeaf(); // True if it doesn't have children

$category1->isSiblingOf($category2); // True if both have the same direct parent

$category1->isParentOf($category2); // True if $category2 parent_id is $category1 id

$category1->isAncestorOf($category2); // True if $category2 is a children (even not a direct children) of $category 1

$category1->isChildOf($category2); // True if $category1 parent_id is $category2 id

$category1->isDescendantOf($category2);// True if $category1 is a children (even not a direct children) of $category 2


$category1->setAsChildOf($category2); // Define $category1 as a direct children of $category2

$category1->setAsParentOf($category2); // Define $category1 as the parent of $category2

$category->setAsRoot(); // Define $category as a root node

$category->setAsChildOf(null); // Alias of $category->setAsRoot();


$category->refreshPath(); // Compute the path and save it (for the current node and all his children)

Category::computeAllPath(); // Compute the path for all the nodes