1. Go to this page and download the library: Download baril/bonsai 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/ */
baril / bonsai example snippets
use Baril\Bonsai\Concerns\BelongsToTree;
class Tag extends Model
{
use BelongsToTree;
protected $parentForeignKey = 'parent_tag_id';
protected $closureTable = 'tag_closures';
}
$tag->parent()->associate($parentTag); // or just: $tag->parent_id = $parentTagId;
$tag->save();
$newParentTag->graft($childTag);
// and:
$childTag->graftOnto($newParentTag);
// are both equivalent to:
$childTag->parent()->associate($newParentTag);
$childTag->save();
$tag->cut();
// is equivalent to:
$tag->parent()->dissociate();
$tag->save();
try {
$tag->delete();
} catch (\Baril\Bonsai\TreeException $e) {
// some specific treatment
// ...
$tag->deleteTree();
}
$tags = Tag::with('descendants')->get();
// The following code won't execute any new query:
foreach ($tags as $tag) {
dump($tag->name);
foreach ($tag->children as $child) {
dump('-' . $child->name);
foreach ($child->children as $grandchild) {
dump('--' . $grandchild->name);
}
}
}
use Baril\Bonsai\Concerns\BelongsToTree;
use Baril\Bonsai\Concerns\SoftDeletes;
class Tag extends Model
{
use BelongsToTree;
use SoftDeletes;
}
try {
$tag->restore();
} catch (\Baril\Bonsai\TreeException $e) {
$tag->cut()->restore(); // will restore $tag as a root
}
use Baril\Bonsai\Concerns\BelongsToTree;
use Baril\Bonsai\Concerns\Orderable;
class Tag extends Model
{
use BelongsToTree;
use Orderable;
protected $orderColumn = 'order';
}