laravel-admin 树形分类

暂无标签

数据表

idaliasnameicondesscriptionsortparent_idcreated_atupdated_atdeleted_at
int(11)varchar(32)varchar(20)varchar(225)varchar(140)int(11)int(11)timestamptimestamptimestamp
ID别名名称图标描述排序父级id创建时间更新时间删除时间

模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use DateTimeInterface;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;

class Node extends Model
{
    use SoftDeletes;
    use ModelTree, AdminBuilder;

    /**
     * 后台
     * 为数组 / JSON 序列化准备日期。
     *
     * @param  \DateTimeInterface  $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    /**
     * 后台
     * 指定数据模型数的关联字段
    */
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('parent_id');
        $this->setOrderColumn('sort');
        $this->setTitleColumn('name');
    }
    
    public function nodes()
    {
        return $this->hasMany(Node::class, 'parent_id' ,'id');
    }

    public function childrenNodes()
    {
        return $this->hasMany(Node::class, 'id')->with('nodes');
    }
}

控制器

<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Facades\DB;
use App\Models\Node;

class NodeController extends Controller
{
    use ModelForm;

    protected $header = '节点管理';

    /**
     * 首页
     * @param Content $content
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description(__('admin.list'));
            $content->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());
                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_base_path('nodes'));
                    $form->select('parent_id', __('admin.parent node'))->options(Node::selectOptions())->rules('required');
                    $form->text('name', __('admin.node name'))->rules('required');
                    $form->textarea('description', __('admin.node description'));
                    $form->number('sort', __('admin.order'))->default(0);
                    $form->hidden('_token')->default(csrf_token());
                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
        });
    }

    protected function treeView()
    {
        return Node::tree(function (Tree $tree) {
            $tree->disableCreate();
            $tree->branch(function ($branch) {
                return "{$branch['sort']} - <strong>{$branch['name']}</strong>"; // 标题添加strong标签
            });
            return $tree;
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->header);
            $content->description(__('admin.edit node'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description('111');
            $content->body($this->form());
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Node::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->select('parent_id', __('admin.parent node'))->options(Node::selectOptions())->rules('required');
            $form->text('name', __('admin.node name'))->rules('required');
            $form->textarea('description', __('admin.node description'));
            $form->number('sort', __('admin.order'));
        });
    }
}