When you call get, you are fetching all this records from the database. And then performing the sum with php. You are probably having a timeout when fetching that much records from the database.
Why don't you perform the sum in the database, and then fetch the result?
public function total()
{
$total = User::limit(100000)->sum('total');
dd($total);
}
----- edit -----
When you perform:
$query = User::limit(100000)->get();
The $query variable actually holds the result set of the query select * from user limit 100000. The result set its a collection (see
collect), then you are performing a sum on the server-side, something equivalent to:
$sum = 0;
foreach ($user in $query) {
$sum = $sum + $user->total;
}
It's better to perform the sum in the database using the proper aggregate function.
$total = User::limit(100000)->sum('total'); would translate to:
select SUM(total) from user limit 1000000
If the result is 0, probably all your result set for the column total is actually 0.