Print a sum in the view

1

MySQL WorkBench

  

ErrorException Array to string conversion

public function listadepontos()
{
    $id = auth()->user()->id;
    $somas = DB::SELECT("select SUM(pontuacao) FROM palpite WHERE id_u = '$id' ");
    return view('pontuacao')->with('somas', $somas);
}

File puntuacao.php

<strong> Sua Pontuação é: <?php echo $somas;?>

</strong>

It worked like this:

@foreach($somas as $s)
  <h4><strong> Sua Pontuação é: {{ $s }}   </strong></h4>
@endforeach

Only once did the punctuation appear, but I do not think it's the right way to code it.

    
asked by anonymous 29.09.2017 / 03:07

2 answers

1
0

Laravel uses Blade Engine to work with templates . Your problem is that your return is objeto , see what documentation tells about the select method to print read the documentation about loops .

  

The select method will always return a series of results. Each result inside the array will be a PHP StdClass object, allowing you to access the results values. (Free Translation)

Then just do loop in View

@foreach ($somas as $soma)
    <p>{{ $soma }}</p>
@endforeach
  

Obs : When answering the question I had put the above code, then I took it out, thinking that I was wrong because I had not tested, before I failed see the edit history.

Another way to return, without having to do loops , is to change the line:

return view('pontuacao')->with('somas', $somas);

To:

return view('pontuacao')->with('somas', $somas[0]['soma']);

And leave in View

{{ $somas }}
    
29.09.2017 / 03:23