Laravel Query Builder - sum() method issue
You don't need to use select()
or get()
when using the aggregate method as sum
:
$purchases = DB::table('transactions') ->join('categories', 'transactions.category_id', '=', 'categories.id') ->where('categories.kind', '=', 1) ->sum('transactions.amount');
If one needs to select SUM
of a column along with a normal selection of other columns, you can sum select that column using DB::raw
method:
DB::table('table_name') ->select('column_str_1', 'column_str_2', DB::raw('SUM(column_int_1) AS sum_of_1')) ->get();
You can get some of any column in Laravel query builder/Eloquent as below.
$data=Model::where('user_id','=',$id)->sum('movement');return $data;
You may add any condition to your record.Thanks