title: How to pass nested foreach from laravel controller to view on blade
body: <p>How to pass nested <code>foreach</code> from <code>laravel</code> controller to view on the blade for a tree structure based in</p>
  
  <p>three levels based on the referende_id on the above code I have fetched the three levels and now I would like to pass the value to the blade in <code>laravel</code> can anyone help me to sort out this.     </p>
  
  <pre class="lang-java prettyprint-override"><code>    class ClientController extends Controller {
       public
       function index($reference_id) {
  
        $clients = table1::leftjoin('table2', function($join) {
         $join -&gt; on('table2.user_id', '=', 'table1.id');
        }) -&gt; where('table1.reference_id', '=', $reference_id) -&gt; get();
  
        foreach($clients as $levelOne) { //here $row is the current $data's collection 
         $clientsLevelOne = table1::leftjoin('table2', function($join) {
          $join -&gt; on('table2.user_id', '=', 'table1.id');
         }) -&gt; where('table1.under_reference', '=', $levelOne -&gt; reference_id) -&gt; get();
  
         //$clientsLevelOne = arrayName["levelOne"];
         //$variable_name['one'] = value;
  
         $datas = array($clientsLevelOne);
  
         foreach($datas as $items) {
          //echo 'LEVEL ONE:&lt;br&gt;&lt;br&gt;';
          foreach($items as $values) {
           $LevelOne = $values;
           //echo $LevelOne.'&lt;br&gt;&lt;br&gt;';
          }
         }
  
         //return $LevelOne;
  
         foreach($datas as $items) { //echo '&lt;br&gt;LEVEL TWO:&lt;br&gt;&lt;br&gt;';
          foreach($items as $values) {
           $levelTwo = $values['reference_id'];
           $clientsLevelTwo = table1::leftjoin('table2', function($join) {
            $join -&gt; on('table2.user_id', '=', 'table1.id');
           }) -&gt; where('table1.under_reference', '=', $levelTwo) -&gt; get();
  
           //dump($clientsLevelTwo);
  
           $LevelDatas = array($clientsLevelTwo);
  
           //$LevelDatas = array_merge($LevelTwo);
           //print_r($LevelDatas);
           //echo '&lt;br&gt;LEVEL TWO:&lt;br&gt;&lt;br&gt;';
  
           foreach($LevelDatas as $two) {
            foreach($two as $values) {
             $levelThree = $values;
            }
           }
          }
         }
  
         foreach($datas as $items) { //echo '&lt;br&gt;LEVEL THREE:&lt;br&gt;&lt;br&gt;';
          foreach($items as $values) {
           $levelThree = $values['reference_id'];
           $clientsLevelThree = table1::leftjoin('table2', function($join) {
            $join -&gt; on('table2.user_id', '=', 'table1.id');
           }) -&gt; where('table1.under_reference', '=', $levelThree) -&gt; get();
  
           $LevelDatas = array($clientsLevelThree);
  
           foreach($LevelDatas as $two) {
            foreach($two as $values) {
             $levelDatasThree = $values['reference_id'];
             $clientsLevelDatasThree = table1::leftjoin('table2', function($join) {
              $join -&gt; on('table2.user_id', '=', 'table1.id');
             }) -&gt; where('table1.under_reference', '=', $levelDatasThree) -&gt; get();
  
             $ThreeDatas = array($clientsLevelDatasThree);
  
             foreach($ThreeDatas as $three) {
              foreach($three as $values) {
               $levelThreeDatas = $values;
              }
             }
            }
           }
          }
         }
        }
  </code></pre>