无限极分类原理 与实现laravel无限极分类

大概思路就是 创建表的时候创建一个他的分类id 这个id则是无限极分类的上级id
在模型里面写的方法就是递归调用 然后在控制器调用模型里面的方法 返回到控制器
大概代码如下

这是在模型里面书写的
Model层

public static function showCity()
	{
   
		// 查看数据
		$info=DB::table('jy_city')->get();
		// 递归调用 自己调用自己
		$result = self::list_level($info,$pid=0,$level=0);
		return $result;
	}
// 写一个提供无限极分类调取的方法
	public static function list_level($info,$pid,$level)
	{
   
		//静态定义一个数组
		static $array=array();
		// 循环
		foreach($info as $k => $v
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Laravel 中,实现无限分类可以使用递归关联来完成。具体来说,我们可以使用一个 `Category` 模型来表示分类,每个分类包含一个 `parent_id` 字段来表示其父分类的 ID。如果 `parent_id` 为 0,则表示该分类为顶级分类。然后,我们可以在 `Category` 模型中定义一个 `children` 方法来表示其子分类,如下所示: ```php class Category extends Model { public function children() { return $this->hasMany(Category::class, 'parent_id'); } } ``` 在上面的代码中,我们使用 `hasMany` 方法来定义 `Category` 模型与自身的一对多关系。具体来说,我们将 `Category` 模型作为第一个参数传递给 `hasMany` 方法,表示我们要关联的模型是 `Category`,然后将 `parent_id` 字段作为第二个参数传递给 `hasMany` 方法,表示我们要使用 `parent_id` 字段来关联两个模型。这样,我们就可以通过 `$category->children` 来获取该分类的所有子分类了。 接下来,我们可以使用递归函数来遍历无限分类。具体来说,我们可以定义一个名为 `getTree` 的静态方法,该方法接受一个 `$parentId` 参数表示要获取的分类的父分类 ID,然后返回一个包含所有子分类分类树数组。实现代码如下: ```php class Category extends Model { public function children() { return $this->hasMany(Category::class, 'parent_id'); } public static function getTree($parentId = 0) { $categories = self::where('parent_id', $parentId)->get(); $tree = []; foreach ($categories as $category) { $tree[] = [ 'id' => $category->id, 'name' => $category->name, 'children' => self::getTree($category->id), ]; } return $tree; } } ``` 在上面的代码中,我们首先使用 `where` 方法来获取指定父分类 ID 的所有分类,然后使用 `foreach` 循环遍历这些分类,构建分类树数组。具体来说,我们将每个分类的 ID 和名称添加到数组中,并递归调用 `getTree` 方法来获取该分类的子分类。最后,我们返回分类树数组。 使用上面的代码,我们可以轻松地获取无限分类。例如,要获取所有顶级分类及其子分类,可以这样写: ```php $categories = Category::getTree(); ``` 要获取指定分类及其子分类,可以这样写: ```php $categories = Category::getTree($categoryId); ``` 其中,`$categoryId` 表示指定分类的 ID。 需要注意的是,上面的代码虽然简单,但在处理大量分类时可能会导致性能问题。因此,建议在数据库中添加一个 `depth` 字段来表示分类的深度,然后在查询分类时使用 `withDepth` 方法来预加载分类的深度,以便更高效地处理大量分类。具体来说,我们可以这样定义 `Category` 模型: ```php class Category extends Model { public function children() { return $this->hasMany(Category::class, 'parent_id'); } public function parent() { return $this->belongsTo(Category::class, 'parent_id'); } public function scopeWithDepth($query) { $table = $this->getTable(); $query->selectRaw("{$table}.*, (SELECT COUNT(*) FROM {$table} AS t WHERE t.id = {$table}.id OR t.{$this->getKeyName()} = {$table}.{$this->getKeyName()}) AS depth"); } } ``` 在上面的代码中,我们定义了一个名为 `withDepth` 的本地作用域,该作用域使用 `selectRaw` 方法来查询分类及其深度。具体来说,我们使用 `COUNT(*)` 函数来统计分类的深度,并将结果保存到 `depth` 字段中。这样,在查询分类时,我们就可以使用 `$query->withDepth()` 方法来预加载分类的深度了。例如,要获取所有分类及其深度,可以这样写: ```php $categories = Category::withDepth()->get(); ``` 获取分类深度后,我们就可以使用递归函数来遍历分类树了。具体来说,我们可以修改 `getTree` 方法,将分类深度作为第二个参数传递给该方法,然后使用 `$category->depth` 来判断分类的深度。如果分类的深度等于指定深度,则将该分类添加到分类树数组中。实现代码如下: ```php class Category extends Model { public function children() { return $this->hasMany(Category::class, 'parent_id'); } public function parent() { return $this->belongsTo(Category::class, 'parent_id'); } public function scopeWithDepth($query) { $table = $this->getTable(); $query->selectRaw("{$table}.*, (SELECT COUNT(*) FROM {$table} AS t WHERE t.id = {$table}.id OR t.{$this->getKeyName()} = {$table}.{$this->getKeyName()}) AS depth"); } public static function getTree($parentId = 0, $depth = null) { $query = self::where('parent_id', $parentId); if ($depth !== null) { $query->where('depth', $depth); } $categories = $query->get(); $tree = []; foreach ($categories as $category) { if ($depth === null || $category->depth == $depth) { $tree[] = [ 'id' => $category->id, 'name' => $category->name, 'children' => self::getTree($category->id, $depth !== null ? $depth + 1 : null), ]; } } return $tree; } } ``` 在上面的代码中,我们首先修改 `getTree` 方法,将分类深度作为第二个参数传递给该方法。然后,在查询分类时,我们使用 `where` 方法来限制分类的深度。如果指定了分类深度,则只查询指定深度的分类;否则,查询所有分类。最后,在遍历分类时,我们使用 `$category->depth` 来判断分类的深度,如果分类的深度等于指定深度,则将该分类添加到分类树数组中。 使用上面的代码,我们可以轻松地获取无限分类。例如,要获取所有顶级分类及其子分类,可以这样写: ```php $categories = Category::getTree(); ``` 要获取指定分类及其子分类,可以这样写: ```php $categories = Category::getTree($categoryId); ``` 其中,`$categoryId` 表示指定分类的 ID。如果要获取指定深度的分类,可以将深度作为第三个参数传递给 `getTree` 方法,例如: ```php $categories = Category::getTree(0, 2); ``` 这样,就可以获取所有顶级分类及其子分类的前两级了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值