2017-04-22 31 views
0

我在將多個變量從控制器傳遞到視圖/刀片時出現問題。即時通訊使用compact()來傳遞變量,但我認爲我已經達到了傳遞變量的限制因爲我已經測試了我的代碼,它會在傳遞30個變量後停止工作。並會給出一個錯誤:「未定義的變量:」,儘管它已經被定義並且完全不運行,除非我傳遞了超過30個變量。我需要傳遞許多變量bcoz我正在創建圖表,並且圖表中的每個值(x,y軸)都是不同的變量。有沒有其他方法可以做到這一點?我正在考慮爲單個視圖使用多個控制器。可能嗎? 總之,這裏是從我的控制器的一個片段:Laravel - 將太多變量傳遞給視圖

$ic15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 0)->where('year_admitted', 2015)->count(); 
    $cas15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 1)->where('year_admitted', 2015)->count(); 
    $ce15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 2)->where('year_admitted', 2015)->count(); 
    $uep15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 3)->where('year_admitted', 2015)->count(); 
    $ced15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 4)->where('year_admitted', 2015)->count(); 
    $cet15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 5)->where('year_admitted', 2015)->count(); 
    $saec15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 6)->where('year_admitted', 2015)->count(); 
    $cgb15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 7)->where('year_admitted', 2015)->count(); 


     return view('success', compact('col','school', 'total', 'f', 'm', 'ic', 'cas', 'ce', 'uep', 'ced', 'cet', 'saec', 'cgb', 'icf', 'icm', 'casf', 'casm', 'cef', 'cem', 'uepf', 'uepm', 'cedf', 'cedm', 'cetf', 'cetm', 'saecf', 'saecm', 'cgbf', 'cgbm', 'ic13', 'cas13', 'ce13', 'uep13', 'ced13', 'cet13', 'saec13', 'cgb13', 'ic14', 'cas14', 'ce14', 'uep14', 'ced14', 'cet14', 'saec14', 'cgb14', 'ic15', 'cas15', 'ce15', 'uep15', 'ced15', 'cet15', 'saec15', 'cgb15')); 

這只是一個片段,其他變量$ic15之前已經被定義。它的定義和使用就像上面顯示的從$ic15$cgb15的其他變量一樣。

回答

1

我不認爲有這樣的限制compact.But您可以創建一個新的數組&把這些值數組中像

$resultArray = array('school' => $school, 'col' => $col); 

,並考慮使用可以得到的數據,從喜歡

print_r($resultArray['school']); 
+0

它解決您的問題,您return語句? –

+0

感謝您的快速解答,先生agam。我必須先嚐試一下你的方法......但是看起來你的代碼正在工作,因爲有人提高了你的答案。我將在後面標記爲答案,如果它能解決我的問題:)) – jeanawhou

+0

當然。希望它適合你 –

0

試試這個:

public function getData($id, $collegeId){ 
    $year = 2015; 
    $school = SchoolStats::where('school_id', $id) 
    ->groupBy('college_id') 
    ->where('college_id','==', $collegeId) 
    ->where('year_admitted','==', $year) 
    ->count(); 
    return view('success', compact('school')); 
} 
0

我想你可以通過變量鑑於這樣的:

return View::make('success', ['col' => $col, 
           'school'=> $school]); 

OR

return View::make('success') 
      ->with('col', $col) 
      ->with('school', $school); 
0

如果你有很多變數儘量擺陣像

$final = array(); 

$ic15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 0)->where('year_admitted', 2015)->count(); 
$cas15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 1)->where('year_admitted', 2015)->count(); 
$ce15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 2)->where('year_admitted', 2015)->count(); 
$uep15 = SchoolStats::where('school_id', $id)->groupBy('college_id')->where('college_id', 3)->where('year_admitted', 2015)->count(); 

$final[] = $ic15; 
$final[] = $cac15; 
$final[] = $ce15; 
$final[] = $uep15; 

再經過一個變量finalcompact

現在鑑於如果你想訪問你的變量

比方說你想訪問$ic15該索引將是0,因爲$ic15是第一個進入數組內部。

{{ $final[0] }} 

如果你想知道的是在什麼位置,然後在最後你的控制器功能

+0

如何從視圖中調用變量? – jeanawhou

+0

它很容易。讓我更新我的答案 –

1

,你可以這樣做使用視圖門面的份額法return view()之前寫dd($final);什麼變量。

$total = SchoolStats::where('school_id', $id)->groupBy('college_id') 
      ->where('college_id', 0)->where('year_admitted', 2015) 
      ->count(); 
$school = SchoolStats::where('school_id', $id)->groupBy('college_id') 
      ->where('college_id', 0)->where('year_admitted', 2015) 
      ->count(); 

view()->share('total',$total); 
view()->share('school',$school); 

return view('success'); 
0

你必須向他們發送括號內,改變類似以下

  return view('success', compact(['col'],['school']));