【问题标题】:Passing hundreds of variables from controller to view in Laravel将数百个变量从控制器传递到 Laravel 中的视图
【发布时间】:2014-04-12 20:15:59
【问题描述】:

这是我的 StatsController:

public function stats()
    {
        $title = "Stats";
        $table = DB::table('stat_data')->get();
        $stats = new \Calculations\Season3\Stats3();
        $stats = $stats->getStats3($table); 
        return View::make('stats')->with('stats', $stats)->with('title',$title);
    }

这是我的应用\Calculations\Season3\Stats3:

<?php namespace Calculations\Season3;

class Stats3
{
    public function getStats3($stats)
    {
        foreach ($stats as $stat) 
                {
                  $variable1 = ...some calculation
                    .
                    .
                    .
                   $variable999 = ...some calculaiton
}

这是我的路线:

 Route::get('stats', 'StatController@stats');

我希望能够在我的 stats.blade.php 视图中的 Stats3 类中使用这些变量并带有回声,
{{ $variable999 }} 我能够计算所有变量,但是当我尝试在其中使用它们时stats.blade.php 我得到一个未定义的变量。以前我可以使用require_once"file" 获取这些变量。我现在想用 MVC/laravel 方法做到这一点,但似乎无法掌握它是如何完成的。

编辑 在 StatsController stats() 我有

$stats = $stats->getStats3($table); 
return View::make('stats')->with('stats', $stats)->with('title',$title);

我现在明白了为什么我不能从我的视图中访问 Stats3() 类中的变量。而且我应该将这些变量存储在一个数组中并将其从控制器传递给视图。构建该数组(将包含数百个变量)并将其传递给视图的最佳方法是什么?

【问题讨论】:

    标签: database view laravel controller


    【解决方案1】:

    你可以简单地:

    foreach ($stats as $stat) 
    {
       View::share('variable1', ...some calculation);
       .
       .
       .
       View::share('variable999', ...some calculation);
    }
    

    您应该能够在视图中使用这些变量。

    【讨论】:

      【解决方案2】:

      我觉得我做不到。我的计算设置与我提出的略有不同

      foreach ($stats as $stat) {
              if($stat->season=="3" && $stat->playoff=="No")
              {
                  if($stat->player=="Chris B"){       
      
          //games played
                      if(!isset($chrisGamesPlayed3)) {
                          $chrisGamesPlayed3=1;
                      } else{     
                          $chrisGamesPlayed3++;
                      }
      
          //wins
                      if($stat->result == "Win") {
                          if(!isset($chrisWins3)) {
                              $chrisWins3=1;
                          } else{
                              ++$chrisWins3;
                          }
                      }
      
          //losses
                      if($stat->result == "Loss") {
                          if(!isset($chrisLoss3)) {
                              $chrisLoss3=1;
                          } else{
                              $chrisLoss3++;
                          }
                      }   
                                .
                                .
                                .
      

      表格是个人游戏统计数据。我所做的计算是赛季平均值。这只是一小部分代码。每个球员都有大约 25 个赛季的统计数据,并且有 8 名球员。看来我的变量太嵌套在 if 语句中,无法执行类似的操作

      foreach ($stats as $stat) 
      {
         View::share('variable1', ...some calculation);
         .
         .
         .
         View::share('variable999', ...some calculation);
      }
      

      【讨论】:

        猜你喜欢
        • 2014-12-08
        • 1970-01-01
        • 1970-01-01
        • 2019-03-30
        • 1970-01-01
        • 2016-01-18
        • 2017-06-06
        • 2012-02-10
        相关资源
        最近更新 更多