根據laravel的基本操做步驟依次完成以下操做:
主要是參考laravel-admin內置的Menu菜單管理的功能,利用ModelTree實現業務中的Tree數據管理。php
1. 建立模型 php artisan make:model Models/Category 2. 建立遷移文件 php artisan make:migration create_categories_table 3. 建立填充文件 php artisan make:seeder CategoriesSeeder 4. 建立後端控制器 php artisan admin:make CategoryController --model=App\Models\Category 5. 建立後端路由 app/admin/routes.php : $router->resource('/web/categories',CategoryController::class); 6. 添加後端菜單 /web/categories:菜單路徑 7. 其餘定義及編輯定製
namespace App\Models; use Encore\Admin\Traits\AdminBuilder; use Encore\Admin\Traits\ModelTree; use Illuminate\Database\Eloquent\Model; class Category extends Model { use ModelTree, AdminBuilder; // protected $fillable = ['name','description','order','parent_id']; public function __construct(array $attributes = []) { parent::__construct($attributes); $this->setParentColumn('parent_id'); $this->setOrderColumn('order'); $this->setTitleColumn('name'); } }
class CreateCategoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('description')->nullable(); $table->integer('order')->unsigned(); $table->integer('parent_id')->unsigned()->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categories'); } }
class CategoriesSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // DB::table('categories')->delete(); for($i = 0; $i < 3; $i++ ){ DB::table('categories')->insert( [ 'name' => 'CAT'.$i, 'description' => 'desc_'.$i, 'order' => $i, 'parent_id' => null ] ); } } }
<?php namespace App\Admin\Controllers; use App\Models\Category; use Encore\Admin\Form; use Encore\Admin\Facades\Admin; use Encore\Admin\Layout\Column; use Encore\Admin\Layout\Content; use App\Http\Controllers\Controller; use Encore\Admin\Controllers\ModelForm; use Encore\Admin\Layout\Row; use Encore\Admin\Tree; use Encore\Admin\Widgets\Box; use Illuminate\Support\Facades\DB; class CategoryController extends Controller { use ModelForm; protected $header = '類型管理'; /** * Index interface. * * @return Content */ public function index() { return Admin::content(function (Content $content) { $content->header($this->header); $content->description('類型列表'); $content->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_base_path('/web/categories')); $form->text('name','類型名稱'); $form->textarea('description','類型描述信息'); $form->number('order','排序序號'); $form->select('parent_id','父類名稱')->options(Category::selectOptions()); $form->hidden('_token')->default(csrf_token()); $column->append((new Box(trans('admin.new'), $form))->style('success')); }); }); }); } protected function treeView() { return Category::tree(function (Tree $tree) { $tree->disableCreate(); return $tree; }); } /** * Edit interface. * * @param $id * @return Content */ public function edit($id) { return Admin::content(function (Content $content) use ($id) { $content->header($this->header); $content->description('編輯類型'); $content->body($this->form()->edit($id)); }); } /** * Create interface. * * @return Content */ public function create() { return Admin::content(function (Content $content) { $content->header($this->header); $content->description('添加類型'); $content->body($this->form()); }); } /** * Make a form builder. * * @return Form */ protected function form() { return Admin::form(Category::class, function (Form $form) { $form->display('id', 'ID'); $form->text('name','類型名稱'); $form->textarea('description','類型描述信息'); $form->number('order','排序序號'); $form->select('parent_id','父類名稱')->options(Category::selectOptions()); }); } public function getCategoryOptions() { return DB::table('categories')->select('id','name as text')->get(); } }
$router->resource('/web/categories',CategoryController::class);
具體操做略
laravel