laravel-admin利用ModelTree实现对分类信息的管理
程序员文章站
2024-02-08 23:21:22
生成模型和迁移文件
php artisan make:model models/shoping/category -m
app/models/shoping/categ...
生成模型和迁移文件
php artisan make:model models/shoping/category -m
app/models/shoping/category.php
<?php namespace app\models\shoping; use encore\admin\traits\adminbuilder; use encore\admin\traits\modeltree; use illuminate\database\eloquent\model; /** * * class category * @package app\models\shoping */ 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=app\models\shoping\category
app/admin/controllers/categoriescontroller.php
use app\models\shoping\category; use encore\admin\controllers\admincontroller; use encore\admin\form; use encore\admin\grid; use encore\admin\layout\column; use encore\admin\layout\content; use encore\admin\layout\row; use encore\admin\show; use encore\admin\tree; use encore\admin\widgets\box; 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);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 入门:CDR中复杂星形工具使用方法
下一篇: HPB芯链受邀出席纽约 “区块+”峰会
推荐阅读