Level 88
You can do something like this
$orders = DB::table('orders')
->select('*', DB::raw('SUM(price) as total_sales'))
->get();
Documentation: https://laravel.com/docs/5.6/queries#raw-expressions
3 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
I want to get all data from a model with name 'user' with a eloquent query and in this query get count of a column and sum of other column, but I won't to divide this action in 3 query...
What should I do?
You can do something like this
$orders = DB::table('orders')
->select('*', DB::raw('SUM(price) as total_sales'))
->get();
Documentation: https://laravel.com/docs/5.6/queries#raw-expressions
Please or to participate in this conversation.