php - Can adding a sum() function be done together with displaying the distinct values I am displaying or should I separate it? if possible, how?

I'm currently trying to learn how databases work in Laravel and encountered a problem and don't know how to fix it or what will be a good solution. Where getting and displaying sum() of column by distinct() id column of one table with other details from the unique id in Laravel.

The data inside my databases are

name email phone address ordered
John John@gmail.com 55982911 St. Louis 2
John John@gmail.com 55982911 St. Louis 1
Mike Mike@gmail.com 55876988 St. Thomas 2
Mike Mike@gmail.com 55876988 St. Thomas 3

Answer

Solution:

You can use groupBy() in Laravel.

$data = reservation::select(array(
    'name', 'email', 'phone', 'address'
))->groupBy(YOUR COLUMN NAME)->get();

Before you use groupBy() make sure you have set strict mode false in the database configuration.

in /config/database.php

'mysql' => [
    // other config values
    'strict' => false,
    // other config values
],

Hope that will helps you to solve your query!!!

Happy Coding!! ????

Source