这篇文章主要介绍了laravel admin实现分类树/模型树,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
修改模型Category.php
<?php
namespace App\Admin\Models;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use ModelTree, AdminBuilder;
protected $table = 'category';
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
//这里根据自己的字段修改
$this->setParentColumn('parent_id');
$this->setOrderColumn('sort');
$this->setTitleColumn('name');
}
}
修改控制文件CategoryController.php
<?php
namespace App\Admin\Controllers;
use App\Admin\Models\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
class CategoryController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = "5ZWG5ZOB5YiG57G7566h55CG";
public function index(Content $content)
{
return Admin::content(function ($content) {
$content->header('商品分类管理');
$content->body(Category::tree(function ($tree) {
$tree->branch(function ($branch) {
$src = config('admin.upload.host') . '/' . $branch['image'];
$logo = "<img src="JHNyYw==" style='max-width:30px;max-height:30px' class='img'/>";
return "{$branch['id']} - {$branch['name']} $logo";
});
}));
});
}
//下面是自己的代码
//.......
}
添加路由app/Admin/routes.php
$router->resource('categories',CategoryController::class);
select中使用分类树
$form->select('parent_id', __('Parent id'))->options(Category::selectOptions())->default(1);
总结
到此这篇关于laravel admin实现分类树/模型树的示例代码的文章就介绍到这了,更多相关laravel admin 分类树 模型树内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:laravel admin实现分类树/模型树的示例代码
猜你喜欢
- PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
- PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
- PHP中PDO事务处理操作示例 2022-10-15
- laravel通用化的CURD的实现 2023-03-17
- PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18
- windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
- 用nohup命令实现PHP的多进程 2023-09-02
- php微信公众号开发之秒杀 2022-11-23
- Laravel balde模板文件中判断数据为空方法 2023-08-30
- laravel实现按月或天或小时统计mysql数据的方法 2023-02-22