title: Laravel API Resource: How to return infinite nested array of categories with their children?
body: <p>I use Laravel 6.x and below is my response JSON. </p>
  
  <pre><code>{
      "data": [
          {
              "id": 1,
              "name": "quam",
              "parent_id": 0
          },
          {
              "id": 2,
              "name": "quia",
              "parent_id": 1
          },
          {
              "id": 3,
              "name": "beatae",
              "parent_id": 1
          },
          {
              "id": 4,
              "name": "aut",
              "parent_id": 2
          },
          {
              "id": 5,
              "name": "provident",
              "parent_id": 0
          },
          {
              "id": 6,
              "name": "voluptate",
              "parent_id": 0
          },
          {
              "id": 7,
              "name": "vel",
              "parent_id": 2
          },
          {
              "id": 8,
              "name": "sed",
              "parent_id": 3
          },
          {
              "id": 9,
              "name": "voluptates",
              "parent_id": 0
          },
          {
              "id": 10,
              "name": "adipisci",
              "parent_id": 6
          },
          ...
      ]
  }
  </code></pre>
  
  <p>But it want to be like this:</p>
  
  <pre><code>{
      "data": [
          {
              "id": 1,
              "name": "quam",
              "children": [
                  {
                     "id": 2,
                     "name": "quam"
                     "children":[
                         {
                             "id": 4,
                             "name": "aut"
                         },
                         {
                             "id": 7,
                             "name": "vel",
                             "children": [
                                  ...
                             ]
                         }
                     ]
                   },
                   {
                     "id": 3,
                     "name": "quam",
                     "children":[
                         {
                             "id": 8,
                             "name": "sed"
                         }
                     ]
                   },
              ]
          },
          {
              "id": 5,
              "name": "provident"
          },
          {
              "id": 6,
              "name": "voluptate",
              "children": [
                   {
                        "id": 10,
                         "name": "adipisci"
                   }
              ]
          },
          {
              "id": 9,
              "name": "voluptates"
          },
          ...
  }
  </code></pre>
  
  <p>In fact, I want to remove the <code>parent_id</code> attribute and add <code>children</code> array to each object that consists of other objects have this parent_id.</p>
  
  <p><strong>CategoryResource.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>class CategoryResource extends JsonResource
  {
      public function toArray($request)
      {
          return [
              'id' =&gt; $this-&gt;id,
              'name' =&gt; $this-&gt;name,
              'parent_id' =&gt; $this-&gt;parent_id,
          ];
      }
  }
  </code></pre>
  
  <p><strong>CategoryController.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>class CategoryController extends Controller
  {
      public function index()
      {
          return CategoryResource::collection(Category::all());
      }
  }
  </code></pre>
  
  <p>How can I implement this structure?</p>