### Install Laravel Tree Package Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Installs the nevadskiy/laravel-tree package using Composer. This is the initial step to integrate the tree functionality into your Laravel project. ```Bash composer require nevadskiy/laravel-tree ``` -------------------------------- ### Getting Products of a Category and its Descendants Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Demonstrates how to retrieve paginated products associated with a category and all its descendants using `HasManyDeep`. ```php $products = $category->products()->paginate(20); ``` -------------------------------- ### Querying Descendants with Materialized Path Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Demonstrates how to query all descendants of a given node using the materialized path pattern in SQL. This example shows a LIKE query for general databases. ```SQL SELECT * FROM categories WHERE path LIKE '1.%' ``` -------------------------------- ### Querying Category Products via Join Source: https://github.com/xalaida/laravel-tree/blob/master/README.md An alternative method to get products of a category and its descendants by joining the `categories` table and using `whereSelfOrDescendantOf()`. ```php $products = Product::query() ->join('categories', function (JoinClause $join) { $join->on('products.category_id', 'categories.id'); }) ->whereSelfOrDescendantOf($category) ->paginate(24, ['products.*']); ``` -------------------------------- ### Ordering Nodes by Depth Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Provides examples for ordering nodes in ascending and descending order based on their depth in the tree. ```php $categories = Category::query()->orderByDepth()->get(); ``` ```php $categories = Category::query()->orderByDepthDesc()->get(); ``` -------------------------------- ### Querying Category Products via WhereHas Source: https://github.com/xalaida/laravel-tree/blob/master/README.md A potentially slower method to get products of a category and its descendants using `whereHas` with a nested `whereSelfOrDescendantOf()` condition. ```php $products = Product::query() ->whereHas('category', function (Builder $query) use ($category) { $query->whereSelfOrDescendantOf($category); }) ->paginate(24); ``` -------------------------------- ### Accessing Ancestors Relation Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Provides examples of accessing all ancestors of a node, both directly via attribute and through the query builder, and joining the current node with its ancestors. ```php foreach ($category->ancestors as $ancestor) { echo $ancestor->name; } ``` ```php $ancestors = $category->ancestors()->get(); ``` ```php $hierarchy = $category->joinAncestors(); ``` -------------------------------- ### Enable PostgreSQL Ltree Migration Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Publishes and runs the package migration to enable the PostgreSQL Ltree extension, which is necessary for using the ltree column type in PostgreSQL databases. ```Bash php artisan vendor:publish --tag=pgsql-ltree-migration ``` -------------------------------- ### Querying Descendants with PostgreSQL Ltree Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Shows how to query all descendants of a given node using PostgreSQL's ltree extension and its specialized query syntax with a GiST index for efficient tree traversal. ```SQL SELECT * FROM categories WHERE path ~ '1.*' ``` -------------------------------- ### Querying Ancestors (Including Self) Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Demonstrates how to query for a node and all its ancestors using the `whereSelfOrAncestorOf()` scope. ```php $ancestors = Category::query()->whereSelfOrAncestorOf($category)->get(); ``` -------------------------------- ### Querying Descendants (Including Self) Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Shows how to query for a node and all its descendants using the `whereSelfOrDescendantOf()` scope. ```php $descendants = Category::query()->whereSelfOrDescendantOf($category)->get(); ``` -------------------------------- ### Defining HasManyDeep Relation for Products Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Shows how to define a `HasManyDeep` relation to link a `Category` model with its descendants' `Product` models. ```php root()->get(); ``` -------------------------------- ### Inserting Root and Child Models Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Demonstrates how to insert a root node by saving a model and a child node by associating it with a parent using the `parent_id` or `parent` relation. ```php $root = new Category(); $root->name = 'Science'; $root->save(); ``` ```php $child = new Category; $child->name = 'Physics'; $child->parent()->associate($root); $child->save(); ``` -------------------------------- ### Moving a Node and its Subtree Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Illustrates how to move a node to a new parent, which automatically updates the `parent_id` and `path` columns for the node and its descendants. ```php $science = Category::query()->where('name', 'Science')->firstOrFail(); $physics = Category::query()->where('name', 'Physics')->firstOrFail(); $physics->parent()->associate($science); $physics->save(); ``` -------------------------------- ### PostgreSQL Migration for Tree Structure Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Defines a database migration for PostgreSQL to create a 'categories' table with an 'id', 'name', 'path' (ltree type), and 'parent_id' column, including a spatial index on the 'path' column. ```PHP id(); $table->string('name'); $table->ltree('path')->nullable()->spatialIndex(); $table->timestamps(); }); Schema::table('categories', function (Blueprint $table) { $table->foreignId('parent_id') ->nullable() ->index() ->constrained('categories') ->cascadeOnDelete(); }); } public function down(): void { Schema::dropIfExists('categories'); } }; ``` -------------------------------- ### Querying Nodes by Depth Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Illustrates how to fetch nodes that are at a specific depth level in the tree using the `whereDepth()` scope. ```php $categories = Category::query()->whereDepth(3)->get(); ``` -------------------------------- ### MySQL/SQLite Migration for Tree Structure Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Defines a database migration for MySQL or SQLite to create a 'categories' table with an 'id', 'name', 'path' (string type), and 'parent_id' column, including an index on the 'path' column. ```PHP $table->string('path')->nullable()->index(); ``` -------------------------------- ### Eloquent Model Configuration Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Configures an Eloquent model to use the tree functionality by using the `AsTree` trait. This automatically handles path manipulations based on the parent-child relationships. ```PHP children as $child) { echo $child->name; } ``` -------------------------------- ### Accessing Descendants Relation Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Demonstrates how to access all descendants of a node, both directly via attribute and through the query builder. ```php foreach ($category->descendants as $descendant) { echo $descendant->name; } ``` ```php $ancestors = $category->descendants()->get(); ``` -------------------------------- ### Build Tree Structure Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Builds a tree structure from a collection of nodes. It associates nodes using the 'children' relation and returns only the root nodes. This is useful for displaying hierarchical data. ```php $tree = Category::query()->orderBy('name')->get()->tree(); ``` -------------------------------- ### Generate Breadcrumbs Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Generates breadcrumbs from a hierarchical data structure. It joins ancestors and formats them into a string, typically separated by a delimiter like ' > '. This is useful for navigation. ```php echo $category->joinAncestors()->reverse()->implode('name', ' > '); ``` -------------------------------- ### Accessing Parent Relation Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Shows how to access the parent of a category model using the `parent` relation provided by the `AsTree` trait. ```php echo $category->parent->name; ``` -------------------------------- ### Delete Subtree Source: https://github.com/xalaida/laravel-tree/blob/master/README.md Deletes a node and all of its descendants from the tree structure. This operation is performed using a query that targets the node and all its descendants. ```php $category->newQuery()->whereSelfOrDescendantOf($category)->delete(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.