这篇文章主要介绍了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实现对分类信息的管理
基础教程推荐
猜你喜欢
- php实现构建排除当前元素的乘积数组方法 2022-11-23
- php数组函数序列之array_sum() – 计算数组元素值之和 2024-01-15
- PHP手机短信验证码实现流程详解 2022-10-18
- Yii框架连表查询操作示例 2023-02-13
- PHP+MySQL+sphinx+scws实现全文检索功能详解 2023-01-31
- PHP实现抽奖系统的示例代码 2023-06-26
- 设定php简写功能的方法 2023-03-17
- PHP实现文件下载【实例分享】 2024-04-27
- PHP判断一个字符串是否是回文字符串的方法 2024-01-31
- php实现数组筛选奇数和偶数示例 2024-02-05
