前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >laravel-admin利用ModelTree实现对分类信息的管理

laravel-admin利用ModelTree实现对分类信息的管理

作者头像
砸漏
发布2020-10-20 10:30:28
7070
发布2020-10-20 10:30:28
举报
文章被收录于专栏:恩蓝脚本

生成模型和迁移文件

代码语言:javascript
复制
php artisan make:model Models/Shoping/Category -m

app/Models/Shoping/Category.php

代码语言:javascript
复制
<?php

namespace App\Models\Shoping;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

/**
 *
 * Class Category
 * @package App\Models\Shoping
 */


class Category extends Model
{
  //
  use ModelTree, AdminBuilder;
  protected $table="shoping_categories";
  public function __construct(array $attributes = [])
  {
    parent::__construct($attributes);
    $this- setTitleColumn("name");
  }
}

迁移文件

代码语言:javascript
复制
class CreateCategoriesTable extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::create('shoping_categories', function (Blueprint $table) {
      $table- increments('id');
      $table- integer('parent_id')- unsigned()- nullable();
      $table- string('name');
      $table- string('description')- nullable();
      $table- integer('order')- unsigned();
      $table- timestamps();
    });
  }

  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::dropIfExists('shoping_categories');
  }
}

生成控制器

代码语言:javascript
复制
php artisan admin:make CategoriesController --model=App\Models\Shoping\Category

app/Admin/Controllers/CategoriesController.php

代码语言:javascript
复制
use App\Models\Shoping\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;

class CategoriesController extends AdminController
{

  public function index(Content $content)
  {
    return $content- title($this- title)
      - description("分类列表")
      - row(function (Row $row) {
        $row- column(6, $this- treeView()- render());
        $row- column(6, function (Column $column) {
          $form = new Form();
          $form- select('parent_id', "父类名称")- options(Category::selectOptions());
          $form- text('name', __('Name'));
          $form- text('description', __('Description'));
          $form- number('order', '排序序号')- default(0);
          $column- append((new Box(trans('admin.new'), $form))- style('success'));
        });

      });

  }

  protected function treeView()
  {
    return Category::tree(function (Tree $tree) {
      $tree- disableCreate();
      return $tree;
    });
  }

添加路由

app/admin/routes.php

代码语言:javascript
复制
$router- resource('categories',CategoryController::class);

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档