laravel 框架有更简单高效的实现方式,无限极分类最佳实践
CREATE TABLE `check_temp_detail` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '模板名称',
`template_id` int unsigned DEFAULT '0' COMMENT '模板id',
`parent_id` int unsigned DEFAULT '0' COMMENT '父级id',
`type` tinyint DEFAULT NULL COMMENT '字段类型',
`required` tinyint DEFAULT '0' COMMENT '是否必填:0否,1是',
`status` tinyint(1) DEFAULT '1' COMMENT '状态:1正常,2禁用,0删除',
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=218 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='检查模板详细字段';
// 模型文件
public function children(): HasMany
{
return $this->hasMany(CheckTempDetail::class, 'parent_id', 'id');
}
public function level(): HasMany
{
return $this->children()->with('level')->orderBy('id', 'asc');
}
// Repositories查询方法
<?php
namespace App\Repositories;
use App\Models\CheckTempDetail;
class CheckTempDetailRepository extends CheckTempDetail
{
public function getCheckTempDetail($templateId): object|null
{
return $this->query()->with('level')->where('template_id', $templateId)->first();
}
}
$data = $this->checkTempDetailRepo->getCheckTempDetail($templateId);
dd($data);
标签: laravel