如果你想要这种样式,请往下继续看。

111.png

首先创建model并且生成初始化迁移文件

php artisan make:model Models/Category -m

接下来我们修改迁移文件,迁移文件在database/migrations/xxxxxx.create)categories_table.php

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('parent_id')->default(0)->comment('父级ID');
            $table->char('title', 200)->default('')->comment('名称');
            $table->unsignedInteger('order')->default(0)->comment('排序字段');
            
//            $table->timestamps();
        });
    }
修改完成执行以下命令运行迁移文件,这时候数据库就生成了数据表。
php artisan migrate

现在我们创建资源控制器并关联模型,添加资源路由到app/Admin/routes.php

php artisan admin:make CategoryController --model=App\\Models\\Category

$router->resource('categories', CategoryController::class);

进入后台系统的菜单编辑页面,添加以下菜单
http://127.0.0.1:8000/admin/auth/menu

22222.png

重点:

1. 在相关的model中添加如下方法和引用类:
<?php

namespace App\Models;

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

class Category extends Model
{
    use ModelTree,AdminBuilder;
    //
}
2. 修改控制器
<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Form as Fo;

class CategoryController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '分类配置';

    /**
     * Make a grid builder.
     *
     * @return mixed
     */
    public function index(Content $content)
    {
        return $content->title(__('分类配置'))
        ->row(function(Row $row) {
            $row->column(6, Category::tree(function ($tree){
                $tree->disableCreate();
                $tree->branch(function ($branch){
                    return $branch['title'];
                });
            }));
            $row->column(6, function ($column){
                $form = new Fo();
                $form->action(admin_base_path('categories'));//请求地址
                $form->select('parent_id', __('admin.parent_id'))->options(Category::selectOptions());
                $form->text('title', __('名称'))->rules('required');
                $form->hidden('_token')->default(csrf_token());
                $column->append((new Box(trans('admin.new'), $form))->style('success'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Category::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('parent_id', __('Parent id'));
        $show->field('title', __('Title'));
        $show->field('order', __('Order'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Category());

        $form->number('parent_id', __('Parent id'));
        $form->text('title', __('Title'));
        $form->text('order', __('Order'));

        return $form;
    }
}

Last modification:October 12, 2020
如果觉得我的文章对你有用,请随意赞赏