Summer Sale! All accounts are 50% off this week.

RadicalActivity's avatar

How to count a column's values?

Hello,

I have a table which has a few columns. One of them contains numbers. Let's say the following. The database has the following values in that column:

2, 5, 8, 5, 10, 15, 4, 6

What I want to do with Laravel is to count these values in the column that has those values.

So the number it should give me back in this case is: 55

With SQL one would do something like this: "SELECT SUM(number_column)".

How is it possible with Eloquent?

0 likes
1 reply
RachidLaasri's avatar
Level 41
$total = DB::table('posts')->sum('views');
$total = Post::sum('views');
18 likes

Please or to participate in this conversation.