这篇文章将为大家详细讲解有关怎么在laravel-admin中利用ModelTree实现对分类信息管理,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
生成模型和迁移文件
php artisan make:model Models/Shoping/Category -m
app/Models/Shoping/Category.php
<?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");
}
}
迁移文件
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');
}
}
生成控制器
php artisan admin:make CategoriesController --model=App\Models\Shoping\Category
app/Admin/Controllers/CategoriesController.php
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
$router->resource('categories',CategoryController::class);
关于怎么在laravel-admin中利用ModelTree实现对分类信息管理就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。