title: Laravel sync users to all of categoires which they can be unlimited
body: <p>In our web application each user can create multiple nested category by selecting the parent which that specified with <code>category_id</code> column.</p>
  <pre class="lang-php prettyprint-override"><code>Schema::create('categories', function (Blueprint $table) {
      $table-&gt;bigIncrements('id');
      $table-&gt;unsignedBigInteger('category_id')-&gt;index()-&gt;nullable();
      $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('categories')-&gt;onDelete('cascade');
      $table-&gt;string('category_name');
  
      ...
  
      $table-&gt;timestamp('created_at')-&gt;useCurrent();
      $table-&gt;timestamp('updated_at')-&gt;useCurrent();
  });
  </code></pre>
  <p>now you supposed one of our user created this category structure:</p>
  <p><a href="https://i.stack.imgur.com/3xjXo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3xjXo.png" alt="enter image description here" /></a></p>
  <p>multiple nested categories children are unlimited, that means every category can be have child and we can't use any static or fixed number for example in <code>for</code> statement to sync the category to specify <code>users</code>.</p>
  <p>my question is how can i set specify <code>users</code> to all of categories from parent?</p>
  <p>this query only can set users for single child of parent. you can suppose <code>1</code>,<code>3</code>,<code>4</code>,<code>5</code>,<code>6</code>,<code>7</code> in above screen shot and not working for <code>2</code> and all of their children</p>
  <p>children of <code>parent category</code> can be unlimited and how can we set <code>users</code> from the parent to all of nested and children categories?</p>
  <pre class="lang-php prettyprint-override"><code>$category = Category::whereId($request-&gt;category_id)-&gt;whereNull('category_id')-&gt;with('childrenCategories')-&gt;first();
  
  $category-&gt;users()-&gt;sync($request-&gt;users);
  foreach ($category-&gt;childrenCategories as $category) {
      $category-&gt;users()-&gt;sync($request-&gt;users);
  }
  </code></pre>
  <p>simplified output of query:</p>
  <pre><code>App\Entities\Category {#2114 ▼
    ...
    #attributes: array:11 [▶]
    #original: array:11 [▶]
    ...
    #relations: array:1 [▼
      &quot;childrenCategories&quot; =&gt; Illuminate\Database\Eloquent\Collection {#2118 ▼
        #items: array:4 [▼
          0 =&gt; App\Entities\Category {#2133 ▶}
          1 =&gt; App\Entities\Category {#2134 ▶}
          2 =&gt; App\Entities\Category {#2135 ▼
            ...
            #attributes: array:11 [▶]
            #original: array:11 [▶]
            ...
            #relations: array:1 [▼
              &quot;categories&quot; =&gt; Illuminate\Database\Eloquent\Collection {#2138 ▼
                #items: array:1 [▼
                  0 =&gt; App\Entities\Category {#2157 ▼
                    ...
                    #attributes: array:11 [▶]
                    #original: array:11 [▶]
                    ...
                    #relations: []
                    ...
                  }
                ]
              }
            ]
            ...
          }
          3 =&gt; App\Entities\Category {#2136 ▶}
        ]
      }
    ]
    ...
  }
  </code></pre>