laravel admin实现分类树/模型树的示例代码
修改模型Category.php <?php namespace AppAdminModels; use EncoreAdminTraitsAdminBuilder; use EncoreAdminTraitsModelTree; use IlluminateDatabaseEloquentModel; 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 AppAdminControllers; use AppAdminModelsCategory; use EncoreAdminControllersAdminController; use EncoreAdminFacadesAdmin; use EncoreAdminLayoutContent; use EncoreAdminShow; 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') . 'http://www.jb51.net/' . $branch['image']; $logo = "<img src='http://www.jb51.net/article/$src'/>"; 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 分类树 模型树内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! (编辑:武汉站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |