This is the 26th day of my participation in the August Text Challenge.More challenges in August

Classification hierarchy list API

1.1 Creating global helper functions

If we go to fetch directly, we will find that the returned data is not like the menu of nested data, of course you can also directly to the front end, let him process, but it is very unfriendly, since we provide API to be robust, friendly, can make THE API call directly used.

Create a global custom helper function in the root directoryheplers.php, you need to create thecomposer.phpTo add auto-loading classes.After adding autoload, you need to refresh and run the commandcomposer dump-autoload:


Heplers.php:


      
use App\Models\Category;

    /** * All category selection properties return */
    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); }); }])// use nested associations to let subclasses find association subclasses
            ->get();
            return $categories; }}/** * The cache is not disabled */
    if(! function_exists('cache_category')) {
        function cache_category () {
            return cache()->rememberForever('cache_category'.function () {
                return categoryTree(1); }); }}/** * caches all categories */
    if(! function_exists('cache_category_all')) {
        function cache_category_all () {
            return cache()->rememberForever('cache_category_all'.function () {
                returncategoryTree(); }); }}/** * Clear all class caches */ 
    if(! function_exists('forget_cache_category_all')) {
        function forget_cache_category_all () {
            cache()->forget('cache_category_all');
            cache()->forget('cache_category'); }}Copy the code

Code parsing:

The categoryTree function accepts a parameter status, either disabled or disabled, or all data if not.


Category::select([‘id’,’pid’,’name’,’level’,’status’]) select(created_at, created_at)


->when($status ! == false, function ($query) use ($status) { $query->where(‘status’, $status); }) perform the filter when $status is passed.


-> select * from (‘pid’, 0) where(‘pid’, 0)


-> < span style = “box-sizing: border-box; color: RGB (74, 74, 74); line-height: 22px; font-size: 14px! Important; word-break: inherit! Important;”


The use of cache here is actually a kind of optimization, there is no need to frequently go to the database query, but it should be noted that after adding a new category, the cache needs to be deleted, so that it will go to the database again.


1.2 Classified API list controller

public function index()
    {   
        return cache_category_all();
    }
Copy the code

And you can see that we’re going to go ahead and take the helper function that we wrote. Note, however, that we need to clear the cache in the add controller method:Also directly calls the helper function we wrote to clear the cache.


1.3 the effect

You can see that we called the request classification list with the following data format (three levels nested) :

[{"id": 9."pid": "0"."name": "Commodity Management"."level": 1."status": 1."children": [{"id": 11."pid": "9"."name": "List of Goods"."level": 2."status": 1."children": []}, {"id": 12."pid": "9"."name": "Add goods"."level": 2."status": 0."children": []}]}, {"id": 10."pid": "0"."name": "Rights Management"."level": 1."status": 1."children": [{"id": 13."pid": "10"."name": "Menu Management"."level": 2."status": 1."children": [{"id": 14."pid": "13"."name": "Add menu"."level": 3."status": 1}]}, {"id": 15."pid": "10"."name": "People management"."level": 2."status": 1."children": [{"id": 16."pid": "15"."name": "Add people"."level": 3."status": 0
                    },
                    {
                        "id": 18."pid": "15"."name": "Delete people"."level": 3."status": 1
                    },
                    {
                        "id": 19."pid": "15"."name": "Delete people"."level": 3."status": 1}]}]Copy the code

If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.