DEV Community

Discussion on: Someone could help me about calculations in blade views?

Collapse
 
alchermd profile image
John Alcher

I suggest you make your views as dumb as possible. What I mean by that is to do all calculations on the controller and only pass the finished data to the view. Ideally, your views contain as little logic as possible and should only concern themselves with presenting the data to the browser. Something like this should illustrate my point:

// Strive to make views as simple as this.
@foreach ($grades as $grade)
    {{ $grade }}
@endforeach

// And do all the heavy lifting in the controller
public function index()
{
    $grades = Grade::someQuery()->all();
    $computed = $grades->map(function ($grade) { /** do your work here */ });

    return view('grades.index', ['grades' => $computed]);
}

If you'd like to share some code, we'd be happy to help some more!

Collapse
 
gustavojordanra profile image
Gustavo Jordan

Thanks I added the code to the post.