将数百个变量从控制器传递到Laravel中查看 [英] Passing hundreds of variables from controller to view in Laravel

查看:148
本文介绍了将数百个变量从控制器传递到Laravel中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的StatsController:

Here is my 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);
    }



这里是我的app\Calculations\Season3\Stats3:

Here is my app\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_oncefile获得这些变量。我现在想使用MVC / laravel方法,但是似乎无法掌握它的工作方式。

I want to be able to use those variables in my Stats3 class in my stats.blade.php view with an echo,
{{ $variable999 }} I am able to calculate all the variables but when I try to use them in stats.blade.php I get an undefined variable. Previously I could get these variables by using require_once"file". I want to do this now with the MVC/laravel method but can't seem to grasp how its done.

编辑
在StatsController stats()中有

Edit In StatsController stats() I have

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

我现在看到为什么我无法从我的视图访问Stats3并且我应该将这些变量存储在一个数组,并将其传递给控制器​​的视图。

I see now why I can't access the variables in the Stats3() class from my view. And that I should store those variables in an array and pass it to the view from the controller. What is the best way to build that array (which will have hundreds of variables) and pass it to the view?

推荐答案

我建立了数组的最佳方法不要以为我可以做到。我的计算设置有点不同,因此我给他们

I don't think I can do that. My calculations are setup a little differently then I presented them

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语句中来做像

The table is the individual game stats. And the calculations I do are season averages. This is just a small piece of the code. Each player has about 25 season stats and there are 8 players. It seems my variables are too nested in if-statements to do something like

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

这篇关于将数百个变量从控制器传递到Laravel中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆