fomvasss / tree-builder
The library for building a tree array structure
Installs: 1 359
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.5.9
This package is auto-updated.
Last update: 2024-11-15 05:46:22 UTC
README
The class for building tree array structure
Installation
Run:
composer require fomvasss/tree-builder
Usage
Create your own class for your item that inherits Fomvasss\TreeBuilder\TreeBuilder
class.
For example, we create CommentTreeItem
and UserTreeItem
:
<?php namespace App\Managers\TreeBuilder; use Fomvasss\TreeBuilder\TreeBuilder; class CommentTreeItem extends TreeBuilder { protected $userTreeItem; public function __construct(UserTreeItem $userTreeItem) { $this->userTreeItem = $userTreeItem; } protected function transform(array $item) { return [ 'id' => $item['id'], 'name' => $item['name'], 'created_at' => optional(\Carbon\Carbon::parse($item['created_at']))->toIso8601String(), 'user' => $this->userTreeItem->getItem($item['user']) ]; } }
In controller we can use next methods:
getTree(array $items)
- get tree structuregetItem(array $item)
- get one itemgetItems(array $items)
- get array itemshideDepth()
- do not show fielddepth
in result arraysetExceptKeys(array $keys)
- set except keys for result array
! Your array must have id
& parent_id
keys for building tree
<?php class CommentController { public function index(Request $request) { $comments = \App\Models\Comment::where('article_id', $request->article_id)->get(); $commentTreeBuilder = new \App\Managers\TreeBuilder\CommentTreeItem(); return \response()->json(['data' => $commentTreeBuilder->getTree($comments->toArray())]); } public function show($id) { $comment = \App\Models\Comment::findOrFail($id); $commentTreeBuilder = new \App\Managers\TreeBuilder\CommentTreeItem(); return \response()->json(['data' => $commentTreeBuilder->hideDepth()->getItem($comment->toArray())]); } }
Example json results:
{ "data": [ { "id": 50, "name": "Larson, Veum and Ondricka", "created_at": "2017-12-23T11:49:02+00:00", "user": { "id": 7, "name": "Brandyn Abbott DVM" }, "children": [ { "id": 73, "name": "Connelly-Zulauf", "created_at": "2017-12-23T11:49:11+00:00", "user": { "id": 9, "name": "Reba Sporer" }, "children": [ { "id": 85, "name": "Ivanov Ivan", "created_at": "2017-12-23T19:15:08+00:00", "user": { "id": 2, "name": "Ned Kunde" }, "children": [], "depth": 3 } ], "depth": 2 } ], "depth": 1 }, { "id": 54, "name": "Gorczany-Swift", "created_at": "2017-12-23T11:49:02+00:00", "user": { "id": 11, "name": "Kevin Legros" }, "children": [], "depth": 1 } ] }