title: how to get nested category parents and siblings of parents?
body: <p>i'm kind of new to laravel eloquent. i'm creating a system were user can add a post/product and select a category for it. when creating the post i use html -select- to show the first level parents then when user picks one using ajax i load the children of that category in another -select- again when user pick one of the childs using ajax i get the children of that category and append theme to form using -select- so when creating a post everything is ok.</p>
  
  <p>the problem is when user wants to edit the post, i cant display list of categories according to the category user has picked.
  for example we have this categories:</p>
  
  <pre><code>-laptop
  -- asus
  ---- 11 inches
  ---- 14 inches
  -- apple
  ---- 11 inches
  ---- 14 inches
  -mobile
  -- samsung
  ---- smart phone
  ---- tablet
  -- apple
  ---- smart phone
  ---- tablet
  </code></pre>
  
  <p><strong>lets say user picked smart phone of apple</strong>. when user is editing the post i want to show categories like this:</p>
  
  <pre><code>&lt;select&gt;
      &lt;option&gt;laptop&lt;/option&gt;
      &lt;option selected&gt;mobile&lt;/option&gt;
  &lt;/select&gt;
  &lt;select&gt;
      &lt;option&gt;samsung&lt;/option&gt;
      &lt;option selected&gt;apple&lt;/option&gt;
  &lt;/select&gt;
  &lt;select&gt;
      &lt;option selected&gt;smart phone&lt;/option&gt;
      &lt;option&gt;tablet&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>notice that all parents of the category that user picked are <strong>selected</strong>.</p>
  
  <p><strong>cats table</strong></p>
  
  <pre><code>id    cat_name    cat_slug    cat_parent
  </code></pre>
  
  <p>i tried to do something like this but guess i got lost in the middle of it.</p>
  
  <pre><code>function getEditedCategories($catId){
     $cats=[];
     $cat=\App\item_category::with('siblings')-&gt;find($catId)-&gt;toArray();
     $cats=array_merge($cats,$cat);
     function getCatParent($catParent,$cats,$i){
         $catParent=\App\item_category::with('siblings')-&gt;find($catParent)-&gt;toArray();
         array_add($cats , 'parents'.$i,$catParent);
         return $catParent['cat_parent'];
     }
     $catParent=$cat['cat_parent'];
     $i=1;
     while(!is_null($catParent)) {
         $i++;
         $catParent = getCatParent($catParent, $cats,$i);
     }
     dd($cats);
  }
  </code></pre>
  
  <p>it's not working and it certainly does not look good.</p>
  
  <p><strong>edited:</strong> the result of dd($cats)</p>
  
  <pre><code>array:5 [
  "id" =&gt; 6
    "cat_name" =&gt; "asgas"
    "cat_slug" =&gt; "asgasdf"
    "cat_parent" =&gt; 5
    "siblings" =&gt; array:1 [
      0 =&gt; array:4 [
        "id" =&gt; 6
        "cat_name" =&gt; "asgas"
        "cat_slug" =&gt; "asgasdf"
        "cat_parent" =&gt; 5
      ]
    ]
  ]
  </code></pre>