Print a sum in the view

Asked

Viewed 331 times

1

In the Mysql Workbench the result comes out, but how to print in View?

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 punctuation.php

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

</strong>

It worked that way:

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

Only once did the score appear, but I don’t think it’s the right way to code.

  • View would be the HTML page?

  • View would be the HTML page? . php html want to print the sum into a "echo"

  • The Laravel uses the Blade Engine to work with templates.

  • can be in tamplate Blade as well!

  • makes a var_dump($somas); instead of echo

  • is there an example? array (size=1) 0 => Object(stdClass)[218] public 'soma' => string '8' (length=1) "...

  • The line $somas = DB::SELECT is returning a array, so that you printed it on View you must change the line view('pontuacao')->with('somas', $somas); for view('pontuacao')->with('somas', $somas[0]);

Show 2 more comments

2 answers

1


Let’s go according to the pure SQL execution documentation from that says the return of a DB::select is an array of stdClass, (in the documentation in English: The select method will always return an array of results. Each result within the array will be a PHP StdClass object, allowing you to access the values of the results:), then the variable data $somas would actually that a array and in its first position a stdClass. In the Controller in the return method, make this change in the :

$sql = "select SUM(pontuacao) as p FROM palpite WHERE id_u=?"
$somas = DB::select($sql, [$id]);
return view('pontuacao')->with('somas', $somas[0]->p);

and consequently in View only:

{{$somas}}

Observing: try sending to your View only what you need to display.

References:

0

The Laravel uses the Blade Engine to work with templates. Your problem is that your return is being a objeto, see what it says to documentation on the method select, to print read the documentation about loops.

The method select will always return a series of results. Each result within the matrix will be an object PHP Stdclass, allowing you to access the values of the results. ( Free translation )

So just do it loop in View

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

Obs: When answering the question I had put the code above, then I took it, for thinking I was wrong because I had not done test, before negative see the editing 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 the View

{{ $somas }}
  • And how did I catch her there in php.blade.?

  • Just as I was before echo $somas;

  • gives error on the line of the Return view('scoring')->with('somas', $somas->soma); Errorexception Trying to get Property of non-object in Playerscontroller.php (line 30)

  • alters to $somas[0] or $somas['soma'] is that where I am or how to test.

  • Fatalerrorexception Cannot use Object of type stdClass as array <? php echo $somas[0];?>

  • I’ll test it here and I’ll get right back to you

  • I think you got it wrong when I told you to change $somas[0] or $somas['soma'] was on the line return view('pontuacao')->with('somas', $somas[0]); and not in the echo $somas; and echo leaves normal or can change to {{ $somas }}.

  • Return view('score')->with('sums', $sums[0]); in view < p >Your score is: {{ $sums }} < p >

  • Errorexception htmlspecialchars() expects Parameter 1 to be string, Object Given (View: C: wamp64 www Games Resources viewsscore.blade.php) in helpers.php (line 547) at Compilerengine->handleViewException(Object(Errorexception), 1) in Phpengine.php (line 44)

  • @foreach($sums up the $s) <H4><Strong> Your Score is: {{ $s }} </Strong></H4><br> @endforeach asim works... as there is only one... but it’s kind of wrong ne?

  • @Daneven I had put so in the answer first, then I thought I was wrong and I took, I will put again and I will leave this one here, because I know it works too, I just could not yet know where I’m erring rs.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.