eaponz
eaponz

Reputation: 604

how can I sum multiple columns in laravel

How can I do this in laravel eloquent, i tried doing this but It gives me error.. how can I sum two columns in laravel SUM( column1 + column2 )

FileDBConsistency::join('Servers', 'Servers.srv_id', '=' , 'FileDBConsistency.srv_id')
        ->sum('SUM( FileDBConsistency.dbconflict + FileDBConsistency.fileconflict ) as sum')
        ->get( $array )

Upvotes: 0

Views: 6520

Answers (1)

Joel Hinz
Joel Hinz

Reputation: 25384

I see two issues:

  1. You don't need to get() the results of a sum() operation.
  2. In order to sum multiple columns, you'll want to use DB::raw().

Here's a Tinker example from the database I have open:

[1] > Priority::sum(DB::raw('priority + priority'));
// '2768'

Upvotes: 3

Related Questions