Hello! There is not resolved by LARAVEL, and specifically a sample of data from the database, I have been fighting for this problem for 2 days = (((How to select data from the database for the current month with the sum of the values ​​of each day (orders may be several in one day), i.e there is such an array

1 => 34$ 1 => 34$ 2 => 45$ 2 => 67$ 3 => 12$ 3 => 42$ 

and the output should be

 1=>68$ 2=>112$ 3=>57$ 

Thanks in advance!

    1 answer 1

     $users = DB::table('data_table_name') ->select(DB::raw('month_field, SUM(price_field) AS sum_field')) ->groupBy('month_field') ->get(); 

    Well, or (which is single-file)

      ->select('month_field', DB::raw('SUM(price_field) AS sum_field')) 
    • I certainly did not correctly formulated the question! I have an order table. The table has id columns, created_at , cost . You need to select data from the table in such a way that a summary of sales for each day is obtained. 1 => 68 $ 2 => 112 $ 3 => $ 57 - Anton
    • Then data_table_name=order , month_field=MONTH(created_at) , price_field=cost . And accordingly in select , and in groupBy it is necessary to use DB::raw . - Akina
    • Thank you so much! You saved my nerves and time))) - Anton