Laravel Admin 树形目录管理

laravel  5.8.38    laravel admin  1.7.16  本文创建于 2020.8.17

完成后长这样

1.创建数据库

CREATE TABLE `art_paper` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `fid` int(11) DEFAULT NULL,
  `index` int(11) DEFAULT NULL,
  `paperid` int(11) DEFAULT NULL,
  `creattime` datetime DEFAULT NULL,
  `state` int(11) DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=utf8;

2.生成model

php artisan code:models –table=art_paper

3.修改Models文件

namespace App\Models;

use Carbon\Carbon;
use Encore\Admin\Traits\ModelTree;  //add
use Illuminate\Database\Eloquent\Model;

class ArtPaper extends Model
{
	use ModelTree;  //add
	protected $table = 'art_paper';
	public $timestamps = false;

/**  add  **/
	public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setParentColumn('fid');
        $this->setOrderColumn('index');
        $this->setTitleColumn('name');
    }
/**  add end **/

4.创建管理页面

php artisan admin:make ArtpaperController --model App\\Models\\ArtPaper

按提示加好路由
Add the following route to app/Admin/routes.php:
$router->resource('art-papers', ArtpaperController::class);

5.修改 Controller 文件

增加
use Encore\Admin\Tree;
use Encore\Admin\Layout\{Column, Row, Content};
use Encore\Admin\Widgets\Box;

//注意这里要新建index方法,在 grid 里写会报错    
 public function index(Content $content){
     	

        return $content
            ->header('逻辑800题目录')
            ->row(function (Row $row){
            	$row->column(6, $this->treeView()->render());
            	$row->column(6, function (Column $column){
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_url('logic-papers'));
                    $form->select('fid', __('父目录'))->options(LogicPaper::selectOptions());
                    $form->text('name', __('名称'))->required();
                    $form->number('index', __('排序'))->default(99)->help('越小越靠前');
                    $form->hidden('_token')->default(csrf_token());
                    $column->append((new Box(__('新建'), $form))->style('success'));
                });
            });
            //->body($tree);

    
    } 
    protected function treeView()
    {
    	 $tree = new Tree(new LogicPaper);
    	 return $tree;
        // return  CategoriesModel::tree(function (Tree $tree){
//             $tree->disableCreate(); // 关闭新增按钮
//             $tree->branch(function ($branch) {
//                 return "<strong>{$branch['cate_name']}</strong>"; // 标题添加strong标签
//             });
//         });
    }

发表评论

电子邮件地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据