Passing hundreds of variables from controller to view in Laravel

大憨熊 提交于 2019-12-25 02:08:54

问题


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);
    }

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
}

Here is my route:

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

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.

Edit In StatsController stats() I have

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

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?


回答1:


You can simply:

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

And you should be able to use those variables in your views.




回答2:


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++;
                    }
                }   
                          .
                          .
                          .

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);
}


来源:https://stackoverflow.com/questions/22288108/passing-hundreds-of-variables-from-controller-to-view-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!