I have a problem in passing multiple variables from a controller to view/blade. Im using compact()
to pass variables but I think i've reached the limit in passing variables coz i have tested my code and it would stop working after passing 30 variables. And would give an error of: "Undefined Variable:" although it is already defined and runs perfectly not unless i pass more than 30 variables. I needed to pass many variables bcoz i am creating a chart and each value from the chart (x, y axis) is a different variable. Is there any alternative way of doing it? Im thinking about using multiple controller for a single view. Is it possible? Anyway, here's a snippet from my controller:
$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'));
This just a snippet, other variables were already defined before $ic15
. It's defined and used just like the other variables from $ic15
to $cgb15
that is shown above.
I don't think that there is such limitation to compact.But you can create a new array & put those values in that array like
$resultArray = array('school' => $school, 'col' => $col);
and in view use can get the data from like
print_r($resultArray['school']);
Try this:
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'));
}
I think you can pass variable in view like:
return View::make('success', ['col' => $col,
'school'=> $school]);
OR
return View::make('success')
->with('col', $col)
->with('school', $school);
If you have many variables try to put them in array like
$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;
And then pass one variable final
in compact
Now in view if you want to access your variable
Lets say you want to access $ic15
the index would be 0
because $ic15
was the first to get inside the array.
{{ $final[0] }}
If you want to know what variable is at what location then write dd($final);
at the end before return view()
of controller function of yours
you may do so using the view facade's share method.
$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');
you have to send them inside brackets, change your return statement like the following one
return view('success', compact(['col'],['school']));