djnew/moonshine-nested-set

Tree resource for moonshine

0.0.8 2024-05-23 15:21 UTC

This package is auto-updated.

Last update: 2025-06-23 17:43:54 UTC


README

The resource tree of this package is based on the kalnoy/nestedset

Requirements

  • MoonShine v2.0+

Installation

composer require djnew/moonshine-nested-set

Get started

Example usage with tree

use Djnew\MoonShineNestedSet\Resources\NestedsetResource;

class CategoryResource extends NestedsetResource
{
    // Required
    protected string $column = 'title';

    protected string $model    = Page::class;

    // Custom child relation name
    public string $treeRelationName = 'children';

    // Use pagination
    protected bool $usePagination = true;

    // Show Up/Down element buttons for sort
    public bool $showUpDownButtons = false;

    // Items per page
    protected int $itemsPerPage = 10;


    protected function pages(): array
    {
        return [
            CategoryTreePage::make($this->title()),
            FormPage::make(
                $this->getItemID()
                    ? __('moonshine::ui.edit')
                    : __('moonshine::ui.add')
            ),
            DetailPage::make(__('moonshine::ui.show')),
        ];
    }

    // ... fields, model, etc ...

    public function treeKey(): ?string
    {
        return 'parent_id';
    }

    // ...
}

And add component

namespace App\MoonShine\Pages;

use Djnew\MoonShineNestedSet\View\Components\NestdSetComponent;
use MoonShine\Pages\Crud\IndexPage;

class CategoryTreePage extends IndexPage
{
    protected function mainLayer(): array
    {
        return [
            ...$this->actionButtons(),
            NestdSetComponent::make($this->getResource()),
        ];
    }
}

Add migration to model

use Kalnoy\Nestedset\NestedSet;

Schema::create('table', function (Blueprint $table) {
    ...
    NestedSet::columns($table);
});

To drop columns:

...
use Kalnoy\Nestedset\NestedSet;

Schema::table('table', function (Blueprint $table) {
    NestedSet::dropColumns($table);
});

Add trait to model

namespace App\Models;

use App\Traits\MoonshineNestedSetTrait;
use Illuminate\Database\Eloquent\Model;


class Page extends Model
{
    use moonshineNestedSetTrait;
    
    // ...
}

Migrating existing data

Migrating from other nested set extension

If your previous extension used different set of columns, you just need to override following methods on your model class:

public function getLftName()
{
    return 'left';
}

public function getRgtName()
{
    return 'right';
}

public function getParentIdName()
{
    return 'parent';
}

// Specify parent id attribute mutator
public function setParentAttribute($value)
{
    $this->setParentIdAttribute($value);
}

Migrating from basic parentage info

If your tree contains parent_id info, you need to add two columns to your schema:

$table->unsignedInteger('_lft');
$table->unsignedInteger('_rgt');

After setting up your model you only need to fix the tree to fill _lft and _rgt columns:

MyModel::fixTree();

Additional content

public function itemContent(Model $item): string
{
    return 'Custom content here';
}

Turn off sortable or wrapable

public function wrapable(): bool
{
    return false;
}

Use async reload tree resource

namespace App\MoonShine\Pages;

use Djnew\MoonShineNestedSet\View\Components\NestdSetComponent;
use MoonShine\Pages\Crud\IndexPage;

class CategoryTreePage extends IndexPage
{
    protected function mainLayer(): array
    {
        return [
            ...$this->actionButtons(),
            Fragment::make(
                [
                    NestdSetComponent::make($this->getResource())
                        ->setFragmentName('fragment-name')
                ]
            )->name('fragment-name')
            ->updateAsync(['page' => request()->get('page')]),
        ];
    }
}```