概述
生成模型和迁移文件
php artisan make:model Models/Shoping/Category -m
app/Models/Shoping/Category.php
<?php namespace AppModelsShoping; use EncoreAdminTraitsAdminBuilder; use EncoreAdminTraitsModelTree; use IlluminateDatabaseEloquentModel; /** * * Class Category * @package AppModelsShoping */ class Category extends Model { // use ModelTree, AdminBuilder; protected $table="shoping_categories"; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->setTitleColumn("name"); } }
迁移文件
class CreateCategoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { 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(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('shoping_categories'); } }
生成控制器
php artisan admin:make CategoriesController --model=AppModelsShopingCategory
app/Admin/Controllers/CategoriesController.php
use AppModelsShopingCategory; use EncoreAdminControllersAdminController; use EncoreAdminForm; use EncoreAdminGrid; use EncoreAdminLayoutColumn; use EncoreAdminLayoutContent; use EncoreAdminLayoutRow; use EncoreAdminShow; use EncoreAdminTree; use EncoreAdminWidgetsBox; class CategoriesController extends AdminController { public function index(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 = new Form(); $form->select('parent_id', "父类名称")->options(Category::selectOptions()); $form->text('name', __('Name')); $form->text('description', __('Description')); $form->number('order', '排序序号')->default(0); $column->append((new Box(trans('admin.new'), $form))->style('success')); }); }); } protected function treeView() { return Category::tree(function (Tree $tree) { $tree->disableCreate(); return $tree; }); }
添加路由
app/admin/routes.php
$router->resource('categories',CategoryController::class);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是聪明鞋垫为你收集整理的laravel-admin利用ModelTree实现对分类信息的管理的全部内容,希望文章能够帮你解决laravel-admin利用ModelTree实现对分类信息的管理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复