一、模型层引入
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
在方法中使用
use ModelTree,AdminBuilder;
最终代码为
<?phpnamespace App\Model;use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
class Category extends Model
{use ModelTree,AdminBuilder;protected $table = 'Category';public function __construct(array $attributes = []){parent::__construct($attributes);$this->setParentColumn('p_id'); // 父ID$this->setOrderColumn('sort'); // 排序$this->setTitleColumn('cate_name'); // 标题}
}
二、控制器层
引入
use App\Model\Category;
use Encore\Admin\Facades\Admin;
之后修改index方法为
public function index(Content $content){return Admin::content(function ($content) {$content->header('商品分类管理');$content->body(Category::tree(function ($tree) {}));});}
修改from方法
protected function form(){$form = new Form(new Category);$form->text('sort', 'sort');$form->text('cate_name', 'cate_name');$form->select('p_id', __('P id'))->options(Category::selectOptions())->default(1);return $form;}
最终效果图