简体   繁体   English

如何从 Laravel 控制器传递嵌套的 foreach 以在刀片上查看

[英]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如何从laravel控制器传递嵌套的foreach以查看基于树结构的刀片

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.基于上述代码中的 referende_id 的三个级别我已经获取了三个级别,现在我想将值传递给laravel的刀片,任何人都可以帮我解决这个问题。

    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.你可以通过 Laravel 的 Eloquent ORM 通过快速和相关的方式实现 3 个级别的关联数据。

First create Model Level1,Level2 And Level3.首先创建模型Level1、Level2和Level3。

To create Relation With Level1 with Level2 in such way:以这种方式创建与 Level1 和 Level2 的关系:

<?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:以这种方式创建与 Level1 和 Level2 的关系:

<?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不要在任何类型的循环中查询,否则在某个阶段您的系统处理时间将受到影响

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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