laravel-admin利用ModelTree实现对分类信息的管理
根据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=App\Models\Category
5. 创建后端路由
app/admin/routes.php : $router->resource('/web/categories',CategoryController::class);
6. 添加后端菜单
/web/categories:菜单路径
7. 其他定义及编辑定制
定义Model文件Category.php
namespace App\Models;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
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
]
);
}
}
}
定义控制器
<?php
namespace App\Admin\Controllers;
use App\Models\Category;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Facades\DB;
class CategoryController extends Controller
{
use ModelForm;
protected $header = '类型管理';
/**
* Index interface.
*
* @return Content
*/
public function index()
{
return Admin::content(function (Content $content) {
$content->header($this->header);
$content->description('类型列表');
$content->row(function (Row $row) {
$row->column(6, $this->treeView()->render());
$row->column(6, function (Column $column) {
$form = new \Encore\Admin\Widgets\Form();
$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);
添加后台菜单
具体操作略
laravel-admin利用ModelTree实现对分类信息的管理的更多相关文章
- [扩展推荐] Laravel 中利用 GeoIP 获取用户地理位置信息
我最近需要一个用户地域检测来设置用户的默认区域和货币.由 Daniel Stainback 创建的 torann/geoip 很好地满足为Laravel 5 项目提供 GeoIP 服务的要求. 这个 ...
- (二十八)分类信息的curd-分类信息删除
删除分类步骤分析: 1.在list.jsp上编写 删除连接 /store/adminCategory?method=delete&cid=?? 2.在delete方法中 获取cid 调用ser ...
- (二十七)分类信息的curd-分类信息修改
修改分类步骤分析: 1.在list.jsp页面上点击修改(编辑) /store/adminCategory?method=getById&cid=??? 2.在getById方法中 获取c ...
- (二十六)分类信息的curd-分类信息添加
分类信息添加: 1.应在在左边的dtree上添加连接(展示所有的分类信息) d.add(...,"/store/adminCategory?method=findAll",&quo ...
- (二十五)后台开发-分类信息的curd -展示所有实现
案例1-分类信息的curd 步骤分析: 左边的dtree: 1.导入dtree.js 2.导入dtree.css 3.创建一个div 添加样式 class="dtree" 4.在d ...
- 织梦CMS首页调用分类信息栏目及列表方法
不懂代码,搜索学习一晚上,都是说调用特定栏目分类信息列表的,用这个代码 {dede:arclistsg row='10' titlelen='24' orderby='pubdate' channel ...
- [转载]织梦CMS首页调用分类信息栏目及列表方法
原文地址:织梦CMS首页调用分类信息栏目及列表方法作者:小武哥 不懂代码,搜索学习一晚上,都是说调用特定栏目分类信息列表的,用这个代码 {dede:arclistsg row='10' titlele ...
- laravel项目利用twemproxy部署redis集群的完整步骤
Twemproxy是一个代理服务器,可以通过它减少Memcached或Redis服务器所打开的连接数.下面这篇文章主要给大家介绍了关于laravel项目利用twemproxy部署redis集群的相关资 ...
- WordPress 获取指定分类ID的分类信息
get_term:直接从数据库中获取分类信息get_the_category:使用post_id作为参数,先根据post_id查询对应的文章然后再返回对应的分类信息,如果没有文章信息则返回Null 之 ...
随机推荐
- linux sig Segmentation fault error
当你发现自己的程序挂了,发现这样的一个报错, 不要慌张,它还是带有一点有用信息的.ps: 如果程序挂了,没有捕抓到这个提示,看一下/var/log/messages对应时间段有没有如下消息.memca ...
- 为什么重写了equals(),还要重写hashCode()?
解决这个问题得先明白:hashCode 方法用于散列集合的查找,equals 方法用于判断两个对象是否相等. 第一步:具体背景(没有背景的讨论就是在耍流氓) 以HashMap中put方法为背景 第二步 ...
- android 面试汇总<二>
Animation Q:Android中有哪几种类型的动画? 技术点:动画类型 参考回答: 常见三类动画 View动画(View Animation)/补间动画(Tween animation):对V ...
- C# 实现播放RTSP 标准协议码流播放
http://www.codeproject.com/Articles/507218/Managed-Media-Aggregation-using-Rtsp-and-Rtphttp://www.st ...
- [Cinder] 存储 Qos
目录 文章目录 目录 前言 操作步骤 参考文章 前言 Cinder 支持 front-end 和 back-end 两种类型的存储 QoS,前者由 Hypervisor 端实现(e.g. 通过 Lib ...
- 十一:jinja2模板传参
从后台传参到模板,模板再渲染到前端 传参的时候,可以在html后面加上关键字传参,在模板里面用{{ 参数 }}使用即可,可以传多个参数 也可以使用**传参,取值的时候就直接取内容
- Mysql数据库事务的四大特性:
什么是事务? 事务Transaction,是指作为一个基本工作单元执行的一系列SQL语句的操作,要么完全地执行,要么完全地都不执行.为什么要使用事务:保证对数据操作的完整性和准确性.1,原子性:一个事 ...
- 考虑以下 Python 代码,如果运行结束,命令行中的运行结果是什么?
l = [] for i in xrange(10): l.append({‘num’:i}) print l在考虑以下代码,运行结束后的结果是什么? l = [] a = {‘num’:0} fo ...
- 自定义Spring-Boot @Enable注解
Spring-Boot中有很多Enable开头的注解,通过添加注解来开启一项功能,如 其原理是什么?如何开发自己的Enable注解? 1.原理 以@EnableScheduling为例,查看其源码,发 ...
- js里面for循环的++i与i++
首先我们应该都知道++i与i++的区别是: ++i 是先执行 i=i+1 再使用 i 的值,而 i++ 是先使用 i 的值再执行 i=i+1: 然后我们也知道for循环的执行顺序如下: for(A;B ...