Monday, February 27, 2017

Calculate value of two columns using Laravel Query

How can I calculate the value of two columns using Laravel 5 query?

Example of my DB structure.

+--------+--------+
| value1 | value2 |
+--------+--------+
| 30     | 30     |
+--------+--------+
| 10     | 10     |
+--------+--------+
| 5      | 25     |
+--------+--------+

I want to get the total value of value1 + value2 I've tried something like this, but it didn't work.

$topUsers = DB::table('users_stats')->orderBy('value1 '+' value2', 'desc')->limit(3)->get();

This way I get this error:

ErrorException in HomeController.php line 39:

A non-numeric value encountered

Expected output:

60
20
30




via Antonio

Advertisement