Using a foreach variable in a Laravel view
Solution 1
change $calculatePercentage = $f-percents; to like below. Then in foreach part change $p->percents to $p.
$calculatePercentage[] = $f->percents;
@foreach($engClassPercentage as $p)
{{$p}}
@endforeach
Solution 2
$calculatePercentage is getting overwritten on each loop change it to an array key
$calculatePercentage[] = $f->percents;
Then in your foreach call the $params array
@foreach($params['engClassPercentage'] as $p)
Michael Naidis
Updated on June 04, 2022Comments
-
Michael Naidis almost 2 years
My MySQL table looks like this (the relevant part):
student_id (int) | engmins(int) | totalmins(int) | created_at (datetime) 1 | 50 | 100 | 2017-12-15 00:00:00 1 | 20 | 45 | 2017-12-15 00:00:00
I have the following code:
$students = StudentDetails::with('student','studentschool','classactivity') ->where('class', 1) ->orderBy('studentgrade', 'DESC') ->get(); $calculatePercentage = array(); foreach ($students as $s) { $fetchEngClassPercents= DB::table('ClassActivity') ->select(DB::raw('round((sum(engmins) * 60) / (sum(totalmins) * 60) * 100) as percents')) ->where('created_at', '>=', Carbon::now()->subDays(30)) ->where('student_id', '=', $s->student->id)->get(); foreach($fetchEngClassPercents as $f) { $calculatePercentage = $f->percents; var_dump($calculatePercentage); //Debug purposes } } var_dump($calculatePercentage); //Debug purposes $params = [ 'engClassPercentage' => $calculatePercentage, 'studentInfo' => $students, ]; return view('user.classes.engclass.index', $params);
- var_dump INSIDE the loop executes string(2) "43" NULL
- var_dump OUTSIDE the loop executes NULL
This is how it looks in the view:
<td> @foreach($engClassPercentage as $p) {{ $p->percents }} @endforeach </td>
This doesn't work in the view, it simply shows nothing.
For some reason, $calculatePercentage remains null outside of the loop (upon previous attempt of dumping the var). When it's in the foreach loop, it executes the query.
The strange thing is that I declared the array ($calculatePercentage) outside of the loop are assigned it with the variable in the foreach loop.
I am quite lost at this point to be honest, and I'd be glad if I can get assistance.
-
Gaurav Gupta over 6 yearstry using it like this return view('user.classes.engclass.index')->with('engClassPercentage' , $calculatePercentage);
-
Michael Naidis over 6 yearsI have other variable that is passed through $resources, how would I do it then?
-
Gaurav Gupta over 6 years$params = [ 'engClassPercentage' => $calculatePercentage, 'studentInfo' => $students, ]; return view('user.classes.engclass.index')->with($params);
-
Michael Naidis over 6 yearsI still seem to be getting "Trying to get property of non-object" in the view while var_dump executes array(1) { [0]=> string(2) "67" }
-
Michael Naidis over 6 yearsThis worked. However, now the same calculation gets applied for all enteries (even if student_id is 2 and he has nothing, it shows the same percentage calculation for student_id) 1.
-
HiKangg over 6 yearsCould you explain more precisely?
-
HiKangg over 6 yearsDo you mean percentage results are same when student_id is 2 and 1, means nothing one and existing one, right?