Using a foreach variable in a Laravel view

12,986

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)
Share:
12,986
Michael Naidis
Author by

Michael Naidis

Updated on June 04, 2022

Comments

  • Michael Naidis
    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
      Gaurav Gupta over 6 years
      try using it like this return view('user.classes.engclass.index')->with('engClassPercentag‌​e' , $calculatePercentage);
    • Michael Naidis
      Michael Naidis over 6 years
      I have other variable that is passed through $resources, how would I do it then?
    • Gaurav Gupta
      Gaurav Gupta over 6 years
      $params = [ 'engClassPercentage' => $calculatePercentage, 'studentInfo' => $students, ]; return view('user.classes.engclass.index')->with($params);
  • Michael Naidis
    Michael Naidis over 6 years
    I 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
    Michael Naidis over 6 years
    This 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
    HiKangg over 6 years
    Could you explain more precisely?
  • HiKangg
    HiKangg over 6 years
    Do you mean percentage results are same when student_id is 2 and 1, means nothing one and existing one, right?