如果你想要这种样式,请往下继续看。
首先创建model并且生成初始化迁移文件
代码语言:javascript复制php artisan make:model Models/Category -m
接下来我们修改迁移文件,迁移文件在database/migrations/xxxxxx.create)categories_table.php
代码语言:javascript复制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
代码语言:javascript复制php artisan admin:make CategoryController --model=App\Models\Category
$router->resource('categories', CategoryController::class);
进入后台系统的菜单编辑页面,添加以下菜单 http://127.0.0.1:8000/admin/auth/menu
重点:
代码语言:javascript复制1. 在相关的model中添加如下方法和引用类:
<?php
namespace AppModels;
use EncoreAdminTraitsAdminBuilder;
use EncoreAdminTraitsModelTree;
use IlluminateDatabaseEloquentModel;
class Category extends Model
{
use ModelTree,AdminBuilder;
//
}
2. 修改控制器
<?php
namespace AppAdminControllers;
use AppModelsCategory;
use EncoreAdminControllersAdminController;
use EncoreAdminForm;
use EncoreAdminLayoutContent;
use EncoreAdminLayoutRow;
use EncoreAdminShow;
use EncoreAdminWidgetsBox;
use EncoreAdminWidgetsForm 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;
}
}