资讯专栏INFORMATION COLUMN

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

olle / 1691人阅读

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

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

</>复制代码

  1. 1. 创建模型
  2. php artisan make:model Models/Category
  3. 2. 创建迁移文件
  4. php artisan make:migration create_categories_table
  5. 3. 创建填充文件
  6. php artisan make:seeder CategoriesSeeder
  7. 4. 创建后端控制器
  8. php artisan admin:make CategoryController --model=AppModelsCategory
  9. 5. 创建后端路由
  10. app/admin/routes.php : $router->resource("/web/categories",CategoryController::class);
  11. 6. 添加后端菜单
  12. /web/categories:菜单路径
  13. 7. 其他定义及编辑定制
定义Model文件Category.php

</>复制代码

  1. namespace AppModels;
  2. use EncoreAdminTraitsAdminBuilder;
  3. use EncoreAdminTraitsModelTree;
  4. use IlluminateDatabaseEloquentModel;
  5. class Category extends Model
  6. {
  7. use ModelTree, AdminBuilder;
  8. //
  9. protected $fillable = ["name","description","order","parent_id"];
  10. public function __construct(array $attributes = [])
  11. {
  12. parent::__construct($attributes);
  13. $this->setParentColumn("parent_id");
  14. $this->setOrderColumn("order");
  15. $this->setTitleColumn("name");
  16. }
  17. }
定义迁移

</>复制代码

  1. class CreateCategoriesTable extends Migration
  2. {
  3. /**
  4. * Run the migrations.
  5. *
  6. * @return void
  7. */
  8. public function up()
  9. {
  10. Schema::create("categories", function (Blueprint $table) {
  11. $table->increments("id");
  12. $table->string("name");
  13. $table->string("description")->nullable();
  14. $table->integer("order")->unsigned();
  15. $table->integer("parent_id")->unsigned()->nullable();
  16. $table->timestamps();
  17. });
  18. }
  19. /**
  20. * Reverse the migrations.
  21. *
  22. * @return void
  23. */
  24. public function down()
  25. {
  26. Schema::dropIfExists("categories");
  27. }
  28. }
填充文件

</>复制代码

  1. class CategoriesSeeder extends Seeder
  2. {
  3. /**
  4. * Run the database seeds.
  5. *
  6. * @return void
  7. */
  8. public function run()
  9. {
  10. //
  11. DB::table("categories")->delete();
  12. for($i = 0; $i < 3; $i++ ){
  13. DB::table("categories")->insert(
  14. [
  15. "name" => "CAT".$i,
  16. "description" => "desc_".$i,
  17. "order" => $i,
  18. "parent_id" => null
  19. ]
  20. );
  21. }
  22. }
  23. }
定义控制器

</>复制代码

  1. header($this->header);
  2. $content->description("类型列表");
  3. $content->row(function (Row $row) {
  4. $row->column(6, $this->treeView()->render());
  5. $row->column(6, function (Column $column) {
  6. $form = new EncoreAdminWidgetsForm();
  7. $form->action(admin_base_path("/web/categories"));
  8. $form->text("name","类型名称");
  9. $form->textarea("description","类型描述信息");
  10. $form->number("order","排序序号");
  11. $form->select("parent_id","父类名称")->options(Category::selectOptions());
  12. $form->hidden("_token")->default(csrf_token());
  13. $column->append((new Box(trans("admin.new"), $form))->style("success"));
  14. });
  15. });
  16. });
  17. }
  18. protected function treeView()
  19. {
  20. return Category::tree(function (Tree $tree) {
  21. $tree->disableCreate();
  22. return $tree;
  23. });
  24. }
  25. /**
  26. * Edit interface.
  27. *
  28. * @param $id
  29. * @return Content
  30. */
  31. public function edit($id)
  32. {
  33. return Admin::content(function (Content $content) use ($id) {
  34. $content->header($this->header);
  35. $content->description("编辑类型");
  36. $content->body($this->form()->edit($id));
  37. });
  38. }
  39. /**
  40. * Create interface.
  41. *
  42. * @return Content
  43. */
  44. public function create()
  45. {
  46. return Admin::content(function (Content $content) {
  47. $content->header($this->header);
  48. $content->description("添加类型");
  49. $content->body($this->form());
  50. });
  51. }
  52. /**
  53. * Make a form builder.
  54. *
  55. * @return Form
  56. */
  57. protected function form()
  58. {
  59. return Admin::form(Category::class, function (Form $form) {
  60. $form->display("id", "ID");
  61. $form->text("name","类型名称");
  62. $form->textarea("description","类型描述信息");
  63. $form->number("order","排序序号");
  64. $form->select("parent_id","父类名称")->options(Category::selectOptions());
  65. });
  66. }
  67. public function getCategoryOptions()
  68. {
  69. return DB::table("categories")->select("id","name as text")->get();
  70. }
  71. }
添加路由

</>复制代码

  1. $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元查看
<