jeudi 21 février 2019

How to sum two columns in Laravel 5.6

I have a SQL query and I want to convert it to eloquent. My query that I know that it works is:

SELECT DISTICT(excavatorId), SUM(times_loaded), SUM(litres) FROM daily GROUP BY excavatorId;

and it returns the following result:

+-------------+-------------------+-------------+
| excavatorId | sum(times_loaded) | sum(litres) |
+-------------+-------------------+-------------+
|          55 |               179 |         168 |
|          60 |                50 |          50 |
+-------------+-------------------+-------------+

Now in Laravel i try the following:

$result = DB::table('daily as d')
                ->select([
                    'excavatorId',
                    'times_loaded',
                    'litres'
                ])
              ->groupBy('excavatorId')
              ->where('date', $request->input('date'))
              ->sum('d.times_loaded', 'd.litres');

This query only returns a string that has the value "179" only. What is the correct way to do this in Laravel and have the result of the SQL query?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire