How to total fields in the blade using Laravel?

0

Expensive, I'm doing dashboard , and I set up a table in HTML , in View and I need to total some columns, and I'd like to do that right in HTML , however since I'm using DB::select I can not use the SUM command.

  • Is there any other way?
  • Can I add within HTML , using some command?
asked by anonymous 15.07.2017 / 01:36

2 answers

0

In Laravel the object returned in the query is Collection . It has a method called sum , which you can use to add specific fields (only the sum is based on the results loaded by PHP, not what is in the database).

$produtos = DB::table('produtos')->select('quantidade', 'preco')->get();

$total_produtos = $produtos->sum('quantidade');

$total_preco = $produtos->sum('preco');

return view('minha_view', compact('total_produtos', 'total_preco');
    
26.07.2017 / 18:56
0

To solve this problem you can put the return of your query into a variable and pass to the view the total of items of this variable. would look something like:

Controller

$dados = DB::select('sua query'); 
$total = $dados->count();
return view('nome da sua view')

In your view you can get the total value this way:

<div> {{$total}} </div>
    
22.07.2017 / 19:12