【问题标题】:Laravel - passing too many variable to a viewLaravel - 将太多变量传递给视图
【发布时间】:2017-05-02 15:39:09
【问题描述】:

我在将多个变量从控制器传递到视图/刀片时遇到问题。我使用compact() 来传递变量,但我认为我已经达到了传递变量的极限,因为我已经测试了我的代码,它会在传递 30 个变量 后停止工作。并且会给出一个错误:“未定义的变量:”尽管它已经定义并且运行完美,除非我传递超过 30 个变量。我需要传递许多变量,因为我正在创建一个图表,图表中的每个值(x,y 轴)都是一个不同的变量。有没有其他方法可以做到这一点?我正在考虑为单个视图使用多个控制器。可能吗? 无论如何,这是我的控制器的一个 sn-p:

    $ic15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 0)->where('year_admitted', 2015)->count();
    $cas15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 1)->where('year_admitted', 2015)->count(); 
    $ce15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 2)->where('year_admitted', 2015)->count(); 
    $uep15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 3)->where('year_admitted', 2015)->count(); 
    $ced15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 4)->where('year_admitted', 2015)->count(); 
    $cet15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 5)->where('year_admitted', 2015)->count(); 
    $saec15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 6)->where('year_admitted', 2015)->count(); 
    $cgb15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 7)->where('year_admitted', 2015)->count(); 


        return view('success', compact('col','school', 'total', 'f', 'm', 'ic', 'cas', 'ce', 'uep', 'ced', 'cet', 'saec', 'cgb', 'icf', 'icm', 'casf', 'casm', 'cef', 'cem', 'uepf', 'uepm', 'cedf', 'cedm', 'cetf', 'cetm', 'saecf', 'saecm', 'cgbf', 'cgbm', 'ic13', 'cas13', 'ce13', 'uep13', 'ced13', 'cet13', 'saec13', 'cgb13', 'ic14', 'cas14', 'ce14', 'uep14', 'ced14', 'cet14', 'saec14', 'cgb14', 'ic15', 'cas15', 'ce15', 'uep15', 'ced15', 'cet15', 'saec15', 'cgb15')); 

这只是一个sn-p,其他变量已经在$ic15之前定义了。它的定义和使用就像上面显示的从 $ic15 到 $cgb15 的其他变量一样。

【问题讨论】:

    标签: php laravel blade laravel-5.4


    【解决方案1】:

    我认为紧凑没有这种限制。但是您可以创建一个新数组并将这些值放入该数组中

    $resultArray = array('school' => $school, 'col' => $col);
    

    在视图中使用可以从like中获取数据

    print_r($resultArray['school']);
    

    【讨论】:

    • 感谢您的快速回答,阿甘先生。我必须先尝试你的方法......但似乎你的代码正在运行,因为有人支持你的答案。我稍后会将此标记为答案,如果它可以解决我的问题:))
    【解决方案2】:

    您可以使用视图外观的 share 方法这样做。

     $total = SchoolStats::where('school_id', $id)->groupBy('college_id')
               ->where('college_id', 0)->where('year_admitted', 2015)
               ->count();
     $school = SchoolStats::where('school_id', $id)->groupBy('college_id')
               ->where('college_id', 0)->where('year_admitted', 2015)
               ->count(); 
    
     view()->share('total',$total);
     view()->share('school',$school);
    
     return view('success');
    

    【讨论】:

      【解决方案3】:

      你必须把它们放在括号里,像下面这样改变你的退货声明

                  return view('success', compact(['col'],['school'])); 
      

      【讨论】:

        【解决方案4】:

        我认为您可以在视图中传递变量,例如:

         return View::make('success', ['col' => $col,
                                       'school'=> $school]);
        

        或

        return View::make('success')
                    ->with('col', $col)
                    ->with('school', $school);
        

        【讨论】:

          【解决方案5】:

          如果你有很多变量,试着把它们放在数组中

          $final = array();
          
           $ic15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 0)->where('year_admitted', 2015)->count();
          $cas15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 1)->where('year_admitted', 2015)->count(); 
          $ce15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 2)->where('year_admitted', 2015)->count(); 
          $uep15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 3)->where('year_admitted', 2015)->count(); 
          
          $final[] = $ic15;
          $final[] = $cac15;
          $final[] = $ce15;
          $final[] = $uep15;
          

          然后在compact中传入一个变量final

          如果你想访问你的变量,现在可以查看

          假设您要访问$ic15,索引将是0,因为$ic15 是第一个进入数组的。

          {{ $final[0] }}
          

          如果你想知道什么变量在什么位置,那么在你的控制器功能的return view()之前写dd($final);

          【讨论】:

          • 如何从视图中调用变量?
          【解决方案6】:

          试试这个:

          public function getData($id, $collegeId){
             $year = 2015;
             $school = SchoolStats::where('school_id', $id)
             ->groupBy('college_id')
             ->where('college_id','==', $collegeId)
             ->where('year_admitted','==', $year)
             ->count();
             return view('success', compact('school'));
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-01-21
            • 2015-07-30
            • 2015-01-11
            • 2016-04-12
            • 1970-01-01
            • 2011-10-01
            • 2019-07-06
            相关资源
            最近更新 更多