2

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.

3
  • try using it like this return view('user.classes.engclass.index')->with('engClassPercentage' , $calculatePercentage); Commented Dec 16, 2017 at 11:18
  • I have other variable that is passed through $resources, how would I do it then? Commented Dec 16, 2017 at 11:22
  • $params = [ 'engClassPercentage' => $calculatePercentage, 'studentInfo' => $students, ]; return view('user.classes.engclass.index')->with($params); Commented Dec 16, 2017 at 12:31

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Could you explain more precisely?
Do you mean percentage results are same when student_id is 2 and 1, means nothing one and existing one, right?
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)

1 Comment

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" }
0

You aren't storing values that is getting calculated in foreach loop:

Hence the values doesn't persist as per your requirements.

Updated Code:

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; // <-- This line is edited.
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.