资讯专栏INFORMATION COLUMN

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

olle / 1358人阅读

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

根据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=AppModelsCategory
5. 创建后端路由
    app/admin/routes.php : $router->resource("/web/categories",CategoryController::class);
6. 添加后端菜单
    /web/categories:菜单路径
7. 其他定义及编辑定制
定义Model文件Category.php
namespace AppModels;

use EncoreAdminTraitsAdminBuilder;
use EncoreAdminTraitsModelTree;
use IlluminateDatabaseEloquentModel;

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");
    }
}
定义迁移
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");
    }
}
填充文件
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
                ]
            );
        }

    }
}
定义控制器
header($this->header);
            $content->description("类型列表");

            $content->row(function (Row $row) {

                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $form = new EncoreAdminWidgetsForm();
                    $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();
    }
}
添加路由
$router->resource("/web/categories",CategoryController::class);
添加后台菜单

具体操作略

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28793.html

相关文章

  • laravel-admin 文件上传OSS

    摘要:前言因为项目需求,需要把图片上传至阿里云,我的接口和后台项目是分开的,都使用的框架开发,接入这里就不做讨论了,这里主要说一下上传阿里的问题。 前言 因为项目需求,需要把图片上传至阿里云 OSS,我的 Api 接口和后台项目是分开的,都使用的 laravel 框架开发,Api 接入 OSS 这里就不做讨论了,这里主要说一下 laravel-admin 上传阿里 OSS 的问题。 网上的一...

    darkbaby123 评论0 收藏0
  • Laravel-admin 爆改(一)

    摘要:爆改一最近再整用了然后爆改了一下记录记录如果觉得不行那就在下面喷吧是一个可以快速帮你构建后台管理的工具,它提供的页面组件和表单元素等功能,能帮助你使用很少的代码就实现功能完善的后台管理功能。 Laravel-admin 爆改(一) 最近再整cms,用了Laravel-admin,然后爆改了一下,记录记录.如果觉得不行,那就在下面喷吧 showImg(https://segmentfau...

    xushaojieaaa 评论0 收藏0
  • laravel-admin 使用记录(三) - 使用扩展

    摘要:导语有一些很方面的扩展可以使用,下面使用管理器。安装其实步骤很简单的,按照官方文档很快就好按照使用两步就安装好了,网址是,看下官方配图数据库选择配置信息命令行都支持,很不错。更多的扩展,可以查看下方链接。 导语 laravel-admin 有一些很方面的扩展可以使用,下面使用Redis 管理器。 安装 其实步骤很简单的,按照官方文档很快就好 composer 按照 composer ...

    bang590 评论0 收藏0

发表评论

0条评论

olle

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<