laravel-admin利用ModelTree实现对分类信息的管理

根据laravel的基本操作步骤依次完成如下操作:主要是参考laravel-admin内置的Menu菜单管理的功能,利用ModelTree实现业务中的Tree数据管理。

1. 创建模型

php artisan make:model Models/Category

2. 创建迁移文件

php artisan make:migration create_categories_table

3. 创建填充文件

php artisan make:seeder CategoriesSeeder

4. 创建后端控制器

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

5. 创建后端路由

app/admin/routes.php : $router->resource('/web/categories',CategoryController::class);

6. 添加后端菜单

/web/categories  //菜单路径

7. 其他定义及编辑定制,定义Model文件Category.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;    //
    protected $fillable = ['name','description','order','parent_id'];
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('parent_id');
        $this->setOrderColumn('order');
        $this->setTitleColumn('name');
    }
}

8.定义迁移

class CreateCategoriesTable extends Migration{    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('description')->nullable();
            $table->integer('order')->unsigned();
            $table->integer('parent_id')->unsigned()->nullable();
            $table->timestamps();
        });
    }    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

9.填充文件

class CategoriesSeeder extends Seeder{    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {        //
        DB::table('categories')->delete();
        for($i = 0; $i < 3; $i++ ){
            DB::table('categories')->insert(
                [ 
                'name' => 'CAT'.$i, 
                'description' => 'desc_'.$i,
                'order' => $i, 
                'parent_id' => null
                ]
            );
        }
    }
}

10.定义控制器

<?
phpnamespace App\Admin\Controllers;
use App\Models\Category;
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;
class CategoryController extends Controller{
use ModelForm;    protected $header = '类型管理';
     /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('类型列表');

            $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('/web/categories'));


                    $form->text('name','类型名称');
                    $form->textarea('description','类型描述信息');
                    $form->number('order','排序序号');
                    $form->select('parent_id','父类名称')->options(Category::selectOptions());
                    
                    $form->hidden('_token')->default(csrf_token());

                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });



        });
    }    protected function treeView()
    {        return Category::tree(function (Tree $tree) {
            $tree->disableCreate();            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('编辑类型');

            $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('添加类型');

            $content->body($this->form());
        });
    }    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {        return Admin::form(Category::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->text('name','类型名称');
            $form->textarea('description','类型描述信息');
            $form->number('order','排序序号');
            $form->select('parent_id','父类名称')->options(Category::selectOptions());


        });
    }    public function getCategoryOptions()
    {        return DB::table('categories')->select('id','name as text')->get();
    }
}

11.添加路由

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

12.添加后台菜单,具体操作略

阅读 292

Comments

匿名

匿名 1个月前

这个 root 根分类是怎么实现的呢,在数据库先插入一条数据还是前端处理

回复此评论