【发布时间】: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