title: Laravel - hiding attributes from json
body: <p>I am trying to hide some attributes from a collection that I get when I query for results in the WordPress database with <a href="https://github.com/corcel/corcel" rel="nofollow noreferrer">corcel</a> package.
  I am getting the menu from the WP database:</p>
  
  <pre><code>$menu = Menu::slug('main-menu')-&gt;first();
  </code></pre>
  
  <p>And then I am trying to sort the menu with submenu, so that it makes sense where is what.</p>
  
  <pre><code>    $res = [];
  
      foreach ($menu-&gt;nav_items as $item) {
          $parent_id = $item-&gt;meta-&gt;_menu_item_menu_item_parent;
          $res[$parent_id][] = $item;
      }
  
      foreach ($res[0] as $item) {
          if (isset($res[$item-&gt;ID])) {
              foreach($res[$item-&gt;ID] as $subItem) {
                  $res[$item-&gt;ID]['subitem'] = $subItem;
              }
          }
      }
  
      return $res;
  </code></pre>
  
  <p>This the data that I get then:</p>
  
  <pre><code>{
      "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
                      }
                  }
              ]
          },
  </code></pre>
  
  <p>And that is just for one menu item, how can I hide some of this attributes?
  I have tried by adding the visible array to the Menu model.</p>
  
  <pre><code>protected $visible = ['ID', 'post_name', 'post_parent', 'guid', 'title', 'meta'];
  </code></pre>
  
  <p>I have also tried with setting the <code>$hidden</code> array but that didn't work either. 
  But, if I do that in the controller this way:</p>
  
  <pre><code>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')-&gt;first();
          $res = [];
  
          foreach ($menu-&gt;nav_items as $item) {
              $item-&gt;makeHidden($this-&gt;hiddenAttributes)-&gt;toArray();
  </code></pre>
  
  <p>Then it works, but that looks ugly, how can I have that I have an array of only visible elements setup in the model?</p>