Lidia Mokevnina
Lidia Mokevnina

Reputation: 125

Change row color depending on $value

I'm trying to highlight table rows with different colors depending on student's average grade. I have if/elseif statements in my controller, but colors are not displayed in the view. What might be wrong?

Here's my controller.

class ResultsController extends Controller
{

    public function index()
    {

        $students = Student::with('subjects')->get();
        $subjects = Subject::all();
        foreach ($students as $student) {
            $average = Point::where('student_id', $student->id)->avg('points');
            $student->avgPoint = $average;

            $color = "#000000";
            if (($student->avgPoint >= 4.5) && ($student->avgPoint <= 5))
                $color = "#10DA3B";
            elseif (($student->avgPoint > 3) && ($student->avgPoint < 4.5))
                $color = "#F0FC36";
            elseif ($student->avgPoint >= 3)
                $color = "#F8280D";

            $student->color = $color;
        }


        return view('results.index', compact('students', 'subjects'));
    }
}

And here's what I have in my blade:

<td><span style="<?php echo $student->color?>">{{ round($student->avgPoint) }}</span></td>

Upvotes: 2

Views: 480

Answers (1)

Emtiaz Zahid
Emtiaz Zahid

Reputation: 2855

Update your blade

<td><span style="{{ $student->color }}">{{ round($student->avgPoint) }}</span></td>

madhur-sharma allready given you the answer in the comment.

I just adding the answer on answer panel

Upvotes: 1

Related Questions