laravel层级分类列表接口

829 阅读3分钟

这是我参与8月更文挑战的第26天,活动详情查看:8月更文挑战

一、分类层级列表api

1.1 创建全局辅助函数

如果我们直接去取的话,会发现返回的数据不会像菜单那种嵌套的数据,当然你也可以直接给前端,让他处理,但是是非常不友好的,我们既然提供api的话就要健壮性,友好性,能够让api调用直接能用的。

1、创建全局自定义辅助函数 在根目录下创建heplers.php,创建完之后需要在composer.php中加入自动加载类。 在这里插入图片描述 添加完自动加载之后,需要去刷新下,运行命令composer dump-autoload在这里插入图片描述


heplers.php写入代码:

<?php
use App\Models\Category;

    /**
     * 所有分类选择属性返回
     */
    if (!function_exists('categoryTree')) {
        function categoryTree ($status=false) { 
            $categories = Category::select(['id','pid','name','level','status'])
            ->when($status !== false, function ($query) use ($status) {
                $query->where('status', $status);
            })
            -> where('pid', 0) 
            ->with([
                'children' => function ($query) use ($status) {
                      $query->select(['id','pid','name','level','status'])
                    ->when($status !== false, function ($query) use ($status) { 
                        $query->where('status', $status);
                    });
                },
                'children.children' => function ($query) use ($status) {
                    $query->select(['id','pid','name','level','status'])
                    ->when($status !== false, function ($query) use ($status) {
                        $query->where('status', $status);
                    });
                }
            ]) // 嵌套关联,让子类去查找关联子类
            ->get();
            return $categories;
        }
    } 

    /**
     * 缓存没被禁用的分类
     */
    if (!function_exists('cache_category')) {
        function cache_category () {
            return cache()->rememberForever('cache_category', function () {
                return categoryTree(1);
            });
        }
    }

    /**
     * 缓存所有的分类
     */
    if (!function_exists('cache_category_all')) {
        function cache_category_all () {
            return cache()->rememberForever('cache_category_all', function () {
                return categoryTree();
            });
        }
    }

    /**
     * 清空所有分类缓存
     */ 
    if (!function_exists('forget_cache_category_all')) {
        function forget_cache_category_all () {
            cache()->forget('cache_category_all');
            cache()->forget('cache_category');
        }
    }

代码解析:

函数categoryTree接受一个参数状态,取禁用或者不禁用,不传的话就是取全部数据。


Category::select(['id','pid','name','level','status'])顶级分类取这几个字段,其他的像created_at都忽略不取。


->when($status !== false, function ($query) use ($status) { $query->where('status', $status); })$status有传的时候才执行筛选操作。


-> where('pid', 0) 在顶级的基础上查找二级,三级。


->with渴求式加载,嵌套关联,让子类去查找关联子类,查找出二级、三级。


这边使用到缓存的话其实是一种优化,不用频繁的去数据库查询,但是需要注意的点是,添加新的分类后是需要删除缓存的,这样才会重新去数据库取。


1.2 分类api列表控制器

public function index()
    {   
        return cache_category_all();
    }

在这里插入图片描述 可以看到我们这里直接去取我们写好的辅助函数。 但是需要注意的是,我们在添加控制器方法中是需要清除缓存的: 在这里插入图片描述 也是直接调用我们写好的清除缓存的辅助函数。


1.3 效果

在这里插入图片描述

可以看到我们调用请求分类列表,其数据格式(三级嵌套)如下:

[
    {
        "id": 9,
        "pid": "0",
        "name": "商品管理",
        "level": 1,
        "status": 1,
        "children": [
            {
                "id": 11,
                "pid": "9",
                "name": "商品列表",
                "level": 2,
                "status": 1,
                "children": []
            },
            {
                "id": 12,
                "pid": "9",
                "name": "添加商品",
                "level": 2,
                "status": 0,
                "children": []
            }
        ]
    },
    {
        "id": 10,
        "pid": "0",
        "name": "权限管理",
        "level": 1,
        "status": 1,
        "children": [
            {
                "id": 13,
                "pid": "10",
                "name": "菜单管理",
                "level": 2,
                "status": 1,
                "children": [
                    {
                        "id": 14,
                        "pid": "13",
                        "name": "添加菜单",
                        "level": 3,
                        "status": 1
                    }
                ]
            },
            {
                "id": 15,
                "pid": "10",
                "name": "人员管理",
                "level": 2,
                "status": 1,
                "children": [
                    {
                        "id": 16,
                        "pid": "15",
                        "name": "添加人员",
                        "level": 3,
                        "status": 0
                    },
                    {
                        "id": 18,
                        "pid": "15",
                        "name": "删除人员",
                        "level": 3,
                        "status": 1
                    },
                    {
                        "id": 19,
                        "pid": "15",
                        "name": "删除人员",
                        "level": 3,
                        "status": 1
                    }
                ]
            }
        ]
    }
]

在学习的php的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。