简体   繁体   中英

How to pass nested foreach from laravel controller to view on blade

How to pass nested foreach from laravel controller to view on the blade for a tree structure based in

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 laravel can anyone help me to sort out this.

    class ClientController extends Controller {
     public
     function index($reference_id) {

      $clients = table1::leftjoin('table2', function($join) {
       $join -> on('table2.user_id', '=', 'table1.id');
      }) -> where('table1.reference_id', '=', $reference_id) -> get();

      foreach($clients as $levelOne) { //here $row is the current $data's collection 
       $clientsLevelOne = table1::leftjoin('table2', function($join) {
        $join -> on('table2.user_id', '=', 'table1.id');
       }) -> where('table1.under_reference', '=', $levelOne -> reference_id) -> get();

       //$clientsLevelOne = arrayName["levelOne"];
       //$variable_name['one'] = value;

       $datas = array($clientsLevelOne);

       foreach($datas as $items) {
        //echo 'LEVEL ONE:<br><br>';
        foreach($items as $values) {
         $LevelOne = $values;
         //echo $LevelOne.'<br><br>';
        }
       }

       //return $LevelOne;

       foreach($datas as $items) { //echo '<br>LEVEL TWO:<br><br>';
        foreach($items as $values) {
         $levelTwo = $values['reference_id'];
         $clientsLevelTwo = table1::leftjoin('table2', function($join) {
          $join -> on('table2.user_id', '=', 'table1.id');
         }) -> where('table1.under_reference', '=', $levelTwo) -> get();

         //dump($clientsLevelTwo);

         $LevelDatas = array($clientsLevelTwo);

         //$LevelDatas = array_merge($LevelTwo);
         //print_r($LevelDatas);
         //echo '<br>LEVEL TWO:<br><br>';

         foreach($LevelDatas as $two) {
          foreach($two as $values) {
           $levelThree = $values;
          }
         }
        }
       }

       foreach($datas as $items) { //echo '<br>LEVEL THREE:<br><br>';
        foreach($items as $values) {
         $levelThree = $values['reference_id'];
         $clientsLevelThree = table1::leftjoin('table2', function($join) {
          $join -> on('table2.user_id', '=', 'table1.id');
         }) -> where('table1.under_reference', '=', $levelThree) -> get();

         $LevelDatas = array($clientsLevelThree);

         foreach($LevelDatas as $two) {
          foreach($two as $values) {
           $levelDatasThree = $values['reference_id'];
           $clientsLevelDatasThree = table1::leftjoin('table2', function($join) {
            $join -> on('table2.user_id', '=', 'table1.id');
           }) -> where('table1.under_reference', '=', $levelDatasThree) -> get();

           $ThreeDatas = array($clientsLevelDatasThree);

           foreach($ThreeDatas as $three) {
            foreach($three as $values) {
             $levelThreeDatas = $values;
            }
           }
          }
         }
        }
       }
      }

You may achieve 3 levels of associated data with Laravel's Eloquent ORM by fast and relevant way.

First create Model Level1,Level2 And Level3.

To create Relation With Level1 with Level2 in such way:

<?php 
// Level 1 relation with Level 2 in App\Level1
public function level2(){
return $this->hasMany('App\Level2','level_id','id');
}
?>

To create Relation With Level1 with Level2 in such way:

<?php 
// Level 2 relation with Level 3 in App\Level2
public function level3(){
return $this->hasMany('App\Level3','level_id','id');
}
?>

and then simply call it in controller's method

<?php
use App\Level1;
     public function index(){
      $levels = Level1::with(['level2.level3'])->get();
      return view('path.file.anyone',compact('levels'));
     }
?>

keep in mind! do not query in any kind of loop otherwise at one stage your system's procressing time will be effected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM