简体   繁体   中英

Passing hundreds of variables from controller to view in Laravel

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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