Question and answer is powered by AnsPress.io
MyModel::select('SUM(price) AS total_price')->where([['id', '=', $request->id]])->get()->toArray();
I have tried with this code to calculate the total price by ID from the MySQL database. But this code did not work for me. How can I calculate the sum of the price by id in Laravel 5?
If you want to calculate the sum of price by id in Laravel, apply this code.
As you code:
MyModel::select(DB::raw('SUM(price) AS total_price'))->where([['id', '=', $request->id]])->get()->toArray();