Laravel - 从json隐藏属性

时间:2017-06-23 13:01:02

标签: json laravel

我正在尝试隐藏我在使用corcel包在WordPress数据库中查询结果时获得的集合中的某些属性。 我从WP数据库中获取菜单:

$menu = Menu::slug('main-menu')->first();

然后我尝试用子菜单对菜单进行排序,这样才有意义。

    $res = [];

    foreach ($menu->nav_items as $item) {
        $parent_id = $item->meta->_menu_item_menu_item_parent;
        $res[$parent_id][] = $item;
    }

    foreach ($res[0] as $item) {
        if (isset($res[$item->ID])) {
            foreach($res[$item->ID] as $subItem) {
                $res[$item->ID]['subitem'] = $subItem;
            }
        }
    }

    return $res;

这是我得到的数据:

{
    "0": [
        {
            "ID": 112,
            "post_author": 1,
            "post_date": "2017-05-12 07:42:16",
            "post_date_gmt": "2017-05-12 05:42:16",
            "post_content": "",
            "post_title": "Hjem",
            "post_excerpt": "",
            "post_status": "publish",
            "comment_status": "closed",
            "ping_status": "closed",
            "post_password": "",
            "post_name": "hjem",
            "to_ping": "",
            "pinged": "",
            "post_modified": "2017-06-23 10:59:47",
            "post_modified_gmt": "2017-06-23 08:59:47",
            "post_content_filtered": "",
            "post_parent": 0,
            "guid": "http://hivnorge.app/?p=112",
            "menu_order": 1,
            "post_type": "nav_menu_item",
            "post_mime_type": "",
            "comment_count": 0,
            "title": "Hjem",
            "slug": "hjem",
            "content": "",
            "type": "nav_menu_item",
            "mime_type": "",
            "url": "http://hivnorge.app/?p=112",
            "author_id": 1,
            "parent_id": 0,
            "created_at": {
                "date": "2017-05-12 07:42:16.000000",
                "timezone_type": 3,
                "timezone": "UTC"
            },
            "updated_at": {
                "date": "2017-06-23 10:59:47.000000",
                "timezone_type": 3,
                "timezone": "UTC"
            },
            "excerpt": "",
            "status": "publish",
            "image": null,
            "terms": {
                "nav_menu": {
                    "hovedmeny": "Hovedmeny"
                }
            },
            "main_category": "Hovedmeny",
            "keywords": [
                "Hovedmeny"
            ],
            "keywords_str": "Hovedmeny",
            "pivot": {
                "term_taxonomy_id": 13,
                "object_id": 112
            },
            "meta": [
                {
                    "meta_id": 208,
                    "post_id": 112,
                    "meta_key": "_menu_item_type",
                    "meta_value": "custom",
                    "value": "custom"
                },
                {
                    "meta_id": 209,
                    "post_id": 112,
                    "meta_key": "_menu_item_menu_item_parent",
                    "meta_value": "0",
                    "value": "0"
                },
                {
                    "meta_id": 210,
                    "post_id": 112,
                    "meta_key": "_menu_item_object_id",
                    "meta_value": "112",
                    "value": "112"
                },
                {
                    "meta_id": 211,
                    "post_id": 112,
                    "meta_key": "_menu_item_object",
                    "meta_value": "custom",
                    "value": "custom"
                },
                {
                    "meta_id": 212,
                    "post_id": 112,
                    "meta_key": "_menu_item_target",
                    "meta_value": "",
                    "value": ""
                },
                {
                    "meta_id": 213,
                    "post_id": 112,
                    "meta_key": "_menu_item_classes",
                    "meta_value": "a:1:{i:0;s:0:\"\";}",
                    "value": [
                        ""
                    ]
                },
                {
                    "meta_id": 214,
                    "post_id": 112,
                    "meta_key": "_menu_item_xfn",
                    "meta_value": "",
                    "value": ""
                },
                {
                    "meta_id": 215,
                    "post_id": 112,
                    "meta_key": "_menu_item_url",
                    "meta_value": "http://hivnorge.app/",
                    "value": "http://hivnorge.app/"
                }
            ],
            "thumbnail": null,
            "taxonomies": [
                {
                    "term_taxonomy_id": 13,
                    "term_id": 13,
                    "taxonomy": "nav_menu",
                    "description": "",
                    "parent": 0,
                    "count": 5,
                    "pivot": {
                        "object_id": 112,
                        "term_taxonomy_id": 13
                    },
                    "term": {
                        "term_id": 13,
                        "name": "Hovedmeny",
                        "slug": "hovedmeny",
                        "term_group": 0
                    }
                }
            ]
        },
        {
            "ID": 136,
            "post_author": 1,
            "post_date": "2017-06-23 10:59:47",
            "post_date_gmt": "2017-06-23 08:59:47",
            "post_content": " ",
            "post_title": "",
            "post_excerpt": "",
            "post_status": "publish",
            "comment_status": "closed",
            "ping_status": "closed",
            "post_password": "",
            "post_name": "136",
            "to_ping": "",
            "pinged": "",
            "post_modified": "2017-06-23 10:59:47",
            "post_modified_gmt": "2017-06-23 08:59:47",
            "post_content_filtered": "",
            "post_parent": 0,
            "guid": "http://hivnorge.app/?p=136",
            "menu_order": 3,
            "post_type": "nav_menu_item",
            "post_mime_type": "",
            "comment_count": 0,
            "title": "",
            "slug": "136",
            "content": " ",
            "type": "nav_menu_item",
            "mime_type": "",
            "url": "http://hivnorge.app/?p=136",
            "author_id": 1,
            "parent_id": 0,
            "created_at": {
                "date": "2017-06-23 10:59:47.000000",
                "timezone_type": 3,
                "timezone": "UTC"
            },
            "updated_at": {
                "date": "2017-06-23 10:59:47.000000",
                "timezone_type": 3,
                "timezone": "UTC"
            },
            "excerpt": "",
            "status": "publish",
            "image": null,
            "terms": {
                "nav_menu": {
                    "hovedmeny": "Hovedmeny"
                }
            },
            "main_category": "Hovedmeny",
            "keywords": [
                "Hovedmeny"
            ],
            "keywords_str": "Hovedmeny",
            "pivot": {
                "term_taxonomy_id": 13,
                "object_id": 136
            },
            "meta": [
                {
                    "meta_id": 334,
                    "post_id": 136,
                    "meta_key": "_menu_item_type",
                    "meta_value": "post_type",
                    "value": "post_type"
                },
                {
                    "meta_id": 335,
                    "post_id": 136,
                    "meta_key": "_menu_item_menu_item_parent",
                    "meta_value": "0",
                    "value": "0"
                },
                {
                    "meta_id": 336,
                    "post_id": 136,
                    "meta_key": "_menu_item_object_id",
                    "meta_value": "127",
                    "value": "127"
                },
                {
                    "meta_id": 337,
                    "post_id": 136,
                    "meta_key": "_menu_item_object",
                    "meta_value": "post",
                    "value": "post"
                },
                {
                    "meta_id": 338,
                    "post_id": 136,
                    "meta_key": "_menu_item_target",
                    "meta_value": "",
                    "value": ""
                },
                {
                    "meta_id": 339,
                    "post_id": 136,
                    "meta_key": "_menu_item_classes",
                    "meta_value": "a:1:{i:0;s:0:\"\";}",
                    "value": [
                        ""
                    ]
                },
                {
                    "meta_id": 340,
                    "post_id": 136,
                    "meta_key": "_menu_item_xfn",
                    "meta_value": "",
                    "value": ""
                },
                {
                    "meta_id": 341,
                    "post_id": 136,
                    "meta_key": "_menu_item_url",
                    "meta_value": "",
                    "value": ""
                }
            ],
            "thumbnail": null,
            "taxonomies": [
                {
                    "term_taxonomy_id": 13,
                    "term_id": 13,
                    "taxonomy": "nav_menu",
                    "description": "",
                    "parent": 0,
                    "count": 5,
                    "pivot": {
                        "object_id": 136,
                        "term_taxonomy_id": 13
                    },
                    "term": {
                        "term_id": 13,
                        "name": "Hovedmeny",
                        "slug": "hovedmeny",
                        "term_group": 0
                    }
                }
            ]
        },

这只是一个菜单项,我该如何隐藏其中一些属性? 我尝试将可见数组添加到Menu模型中。

protected $visible = ['ID', 'post_name', 'post_parent', 'guid', 'title', 'meta'];

我也试过设置$hidden数组,但这也不起作用。 但是,如果我这样在控制器中这样做:

private $hiddenAttributes = [
      'post_author',
      'post_date',
      'content',
      'type',
      'post_type',
      'post_date_gmt',
      'post_content',
      'post_excerpt',
      'comment_status',
      'ping_status',
      'post_password',
      'to_ping',
      'pinged',
      'post_modified',
      'post_modified_gmt',
      'post_content_filtered',
      'post_mime_type',
      'comment_count',
      'mime_type',
      'created_at',
      'updated_at',
      'excerpt',
    ];

    public function index()
    {
        $menus = Menu::all();
        $menu = Menu::slug('hovedmeny')->first();
        $res = [];

        foreach ($menu->nav_items as $item) {
            $item->makeHidden($this->hiddenAttributes)->toArray();

然后它可以工作,但看起来很难看,我怎么能拥有模型中只设置可见元素的数组呢?

1 个答案:

答案 0 :(得分:1)

$hidden数组添加到您要隐藏的列的菜单模型中。

示例:

protected $hidden = [
    'created_at', 'updated_at', 'deleted_at', 'user_id'
];