TAY
笔记 · · 阅读 223
更新于

Laravel-admin 树形无限级分类

Model

<?php

namespace App\Models;

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

class AiCategory extends Model
{
    use ModelTree, AdminBuilder;
    
    public $timestamps = false;
    
    protected $with = [
        'parent'
    ];
    
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('parent_id');  // 父ID
        $this->setOrderColumn('sort'); // 排序
        $this->setTitleColumn('name'); // 标题
    }

    /**
     * 该分类的子分类
     */
    public function child()
    {
        return $this->hasMany(get_class($this), 'parent_id', $this->getKeyName());
    }
 
    /**
     * 该分类的父分类
     */
    public function parent()
    {
        return $this->hasOne(get_class($this), $this->getKeyName(), 'parent_id');
    }
}

Controller

<?php
 
namespace App\Admin\Controllers;

use App\Models\AiCategory;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Layout\{Column, Row, Content};
use Encore\Admin\{Tree,Form};
use Encore\Admin\Widgets\Box;
use Illuminate\Http\RedirectResponse;

/**
 * 分类管理
 */
class AiCategoryController extends Content
{
    use HasResourceActions;

    protected $title = '分类';

    /**
     * 首页
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content->title('分类')
            ->description('列表')
            ->row(function (Row $row){
                // 显示分类树状图
                $row->column(6, $this->treeView()->render());
 
                $row->column(6, function (Column $column){
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->select('parent_id', __('父类'))->options(AiCategory::selectOptions());
                    $form->text('name', __('分类名称'))->required();
                    $form->text('icon', __('图标'));
                    $form->number('sort', __('排序'))->min(0)->default(0)->help('越小越靠前');
                    $form->hidden('_token')->default(csrf_token());
                    $column->append((new Box(__('分类'), $form))->style('success'));
                });
 
            });
    }

    /**
     * 树状视图
     * @return Tree
     */
    protected function treeView()
    {
        return  AiCategory::tree(function (Tree $tree){
            $tree->disableCreate(); // 关闭新增按钮
            $tree->branch(function ($branch) {
                $icon = $branch['icon'] ? "<div style='width:15px;height:15px;margin-right:0.5rem;display:inline-flex;'>{$branch['icon']}</div>" : '';
                return "<div style='display:inline-flex;align-items:center;'>{$icon}<strong>{$branch['name']}</strong></div>"; // 标题添加strong标签
            });
        });
    }
 
    /**
     * 详情页
     * @param $id
     * @return RedirectResponse
     */
    public function show($id)
    {
        return redirect()->route('AiCategorys', ['id' => $id]);
    }
 
    /**
     * 编辑
     * @param $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content->title(__('Categories'))
            ->description(__('edit'))
            ->row($this->form()->edit($id));
    }
 
 
    /**
     * 表单
     * @return Form
     */
    public function form()
    {
        $form = new Form(new AiCategory());
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });
        $form->display('id', 'ID');
        $form->select('parent_id', __('父类'))->options(AiCategory::selectOptions());
        $form->text('name', __('分类名称'))->required();
        $form->text('icon', __('图标'));
        $form->number('sort', __('排序'))->min(0)->default(0)->help('越小越靠前');
        return $form;
    }
}

目录