laravel-admin利用ModelTree实现对分类信息的管理
生成模型和迁移文件
phpartisanmake:modelModels/Shoping/Category-m
app/Models/Shoping/Category.php
setTitleColumn("name"); } }
迁移文件
classCreateCategoriesTableextendsMigration { /** *Runthemigrations. * *@returnvoid */ publicfunctionup() { Schema::create('shoping_categories',function(Blueprint$table){ $table->increments('id'); $table->integer('parent_id')->unsigned()->nullable(); $table->string('name'); $table->string('description')->nullable(); $table->integer('order')->unsigned(); $table->timestamps(); }); } /** *Reversethemigrations. * *@returnvoid */ publicfunctiondown() { Schema::dropIfExists('shoping_categories'); } }
生成控制器
phpartisanadmin:makeCategoriesController--model=App\Models\Shoping\Category
app/Admin/Controllers/CategoriesController.php
useApp\Models\Shoping\Category; useEncore\Admin\Controllers\AdminController; useEncore\Admin\Form; useEncore\Admin\Grid; useEncore\Admin\Layout\Column; useEncore\Admin\Layout\Content; useEncore\Admin\Layout\Row; useEncore\Admin\Show; useEncore\Admin\Tree; useEncore\Admin\Widgets\Box; classCategoriesControllerextendsAdminController { publicfunctionindex(Content$content) { return$content->title($this->title) ->description("分类列表") ->row(function(Row$row){ $row->column(6,$this->treeView()->render()); $row->column(6,function(Column$column){ $form=newForm(); $form->select('parent_id',"父类名称")->options(Category::selectOptions()); $form->text('name',__('Name')); $form->text('description',__('Description')); $form->number('order','排序序号')->default(0); $column->append((newBox(trans('admin.new'),$form))->style('success')); }); }); } protectedfunctiontreeView() { returnCategory::tree(function(Tree$tree){ $tree->disableCreate(); return$tree; }); }
添加路由
app/admin/routes.php
$router->resource('categories',CategoryController::class);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。