Laravel admin 實(shí)現(xiàn)分類樹/模型樹

Laravel admin 實(shí)現(xiàn)分類樹/模型樹

修改模型Category.php

<?php namespace AppAdminModels;  use EncoreAdminTraitsAdminBuilder; use EncoreAdminTraitsModelTree; use IlluminateDatabaseEloquentModel;  class Category extends Model {  use ModelTree, AdminBuilder;   protected $table = &#39;category&#39;;   public function __construct(array $attributes = [])  {   parent::__construct($attributes); 		//這里根據(jù)自己的字段修改   $this->setParentColumn('parent_id'); ??$this-&gt;setOrderColumn('sort'); ??$this-&gt;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 = &#39;商品分類管理&#39;;   public function index(Content $content)  {   return Admin::content(function ($content) {    $content->header('商品分類管理'); ???$content-&gt;body(Category::tree(function?($tree)?{ ????$tree-&gt;branch(function?($branch)?{ ?????$src?=?config('admin.upload.host')?.?'/'?.?$branch['image']; ?????$logo?=?"@@##@@";  ?????return?"{$branch['id']}?-?{$branch['name']}?$logo"; ????}); ???})); ??}); ?} 	//下面是自己的代碼 	//....... ?}

添加路由app/Admin/routes.php

$router-&gt;resource('categories',CategoryController::class);

select中使用分類樹

$form-&gt;select('parent_id',?__('Parent?id'))-&gt;options(Category::selectOptions())-&gt;default(1);

推薦教程:《Laravel教程

Laravel admin 實(shí)現(xiàn)分類樹/模型樹

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊11 分享