Home  >  Article  >  PHP Framework  >  Laravel admin implements classification tree/model tree

Laravel admin implements classification tree/model tree

Guanhui
Guanhuiforward
2020-06-20 18:05:134770browse

Laravel admin implements classification tree/model tree

Modify model 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 = &#39;category&#39;;

 public function __construct(array $attributes = [])
 {
  parent::__construct($attributes);
		//这里根据自己的字段修改
  $this->setParentColumn(&#39;parent_id&#39;);
  $this->setOrderColumn(&#39;sort&#39;);
  $this->setTitleColumn(&#39;name&#39;);
 }
 }

Modify control file 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 = &#39;商品分类管理&#39;;

 public function index(Content $content)
 {
  return Admin::content(function ($content) {
   $content->header(&#39;商品分类管理&#39;);
   $content->body(Category::tree(function ($tree) {
    $tree->branch(function ($branch) {
     $src = config(&#39;admin.upload.host&#39;) . &#39;/&#39; . $branch[&#39;image&#39;];
     $logo = "<img src=&#39;$src&#39; style=&#39;max-width:30px;max-height:30px&#39; class=&#39;img&#39;/>";

     return "{$branch[&#39;id&#39;]} - {$branch[&#39;name&#39;]} $logo";
    });
   }));
  });
 }
	//下面是自己的代码
	//.......
 }

Add routing app/Admin/routes.php

$router->resource(&#39;categories&#39;,CategoryController::class);

Using classification trees in select

$form->select(&#39;parent_id&#39;, __(&#39;Parent id&#39;))->options(Category::selectOptions())->default(1);

Recommended tutorial: "Laravel Tutorial"

The above is the detailed content of Laravel admin implements classification tree/model tree. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete