laravel admin实现分类树/模型树的示例代码
程序员文章站
2022-06-25 08:06:23
修改模型category.php
修改模型category.php
<?php namespace app\admin\models; use encore\admin\traits\adminbuilder; use encore\admin\traits\modeltree; use illuminate\database\eloquent\model; class category extends model { use modeltree, adminbuilder; protected $table = 'category'; public function __construct(array $attributes = []) { parent::__construct($attributes); //这里根据自己的字段修改 $this->setparentcolumn('parent_id'); $this->setordercolumn('sort'); $this->settitlecolumn('name'); } }
修改控制文件categorycontroller.php
<?php namespace app\admin\controllers; use app\admin\models\category; use encore\admin\controllers\admincontroller; use encore\admin\facades\admin; use encore\admin\layout\content; use encore\admin\show; class categorycontroller extends admincontroller { /** * title for current resource. * * @var string */ protected $title = '商品分类管理'; public function index(content $content) { return admin::content(function ($content) { $content->header('商品分类管理'); $content->body(category::tree(function ($tree) { $tree->branch(function ($branch) { $src = config('admin.upload.host') . '/' . $branch['image']; $logo = "<img src='$src' style='max-width:30px;max-height:30px' class='img'/>"; return "{$branch['id']} - {$branch['name']} $logo"; }); })); }); } //下面是自己的代码 //....... }
添加路由app/admin/routes.php
$router->resource('categories',categorycontroller::class);
select中使用分类树
$form->select('parent_id', __('parent id'))->options(category::selectoptions())->default(1);
总结
到此这篇关于laravel admin实现分类树/模型树的示例代码的文章就介绍到这了,更多相关laravel admin 分类树 模型树内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: js验证账户名是否重复