当前位置 : 主页 > 编程语言 > 其它开发 >

Laravel admin 实现分类树/模型树

来源:互联网 收集:自由互联 发布时间:2021-08-14
修改模型Category.php ?phpnamespace 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 $tabl

修改模型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教程》

以上就是Laravel admin 实现分类树/模型树的详细内容,更多请关注自由互联其它相关文章!

网友评论