1

I want to calculate all numbers in one row from table and get the resulting in blade, how can ? i'm using Laravel 5.3? this my try:

{{ $posts = App\Post::where(['counts' => allOf()])->count() }}

and this:

{{ $posts = App\Post::orderBy('counts', '=', '*')->count() }}

but this not working

Mohamed Hamdy
  • 57
  • 2
  • 9

1 Answers1

2

It sounds like you want the SUM of the count?

If you are using the query builder directly you could use sum('count').

https://laravel.com/docs/5.3/queries#aggregates

Since you appear to be using eloquent you should use App\Post::all()->sum('count')

https://laravel.com/docs/5.3/collections#method-sum

This will gather all the posts into a collection and then sum the count field.

Edit...

Might also try App\Post::sum('count')

aknosis
  • 3,602
  • 20
  • 33
  • If App\Post::sum('count') works, that is the most performant option. This will run a sum() query at the database level rather than all()->sum() which would have to query all rows and then sum the count in php. – aknosis Sep 15 '17 at 15:58