0

I am using Laravel 8 and i want to get data of each month of the year. For example

month1 => 12
month2 => 14 so on ...

I have code

 $invoices = Auth::user()->invoices()
        ->select(DB::raw('YEAR(invoice_date) year'), DB::raw('MONTH(invoice_date) month'), DB::raw('count(id) as `data`'))
        ->groupBy('year','month')
        ->get()->toArray();

it return following,

 array:2 [▼
1 => array:3 [▼
"year" => 2021
"month" => 1
"data" => 6
]
2 => array:3 [▼
"year" => 2021
"month" => 2
"data" => 4

]]

but i have record in database 20. mean for month 1 record are 12 and for month 2 record are 8 but i get different. Kindly help me where is i am wrong. Table structure is enter image description here

Irshad Khan
  • 161
  • 2
  • 15
  • could you add the table structure and model definition? – V-K Jan 05 '21 at 12:13
  • I have updated my question please have a look. Thanks for your time. – Irshad Khan Jan 05 '21 at 12:15
  • The code seems good. Add please `DB::enableQueryLog();` before and `Log::info(var_export(DB::getQueryLog(), true));` after the code. Than you can find sql query in the logs, add it to post – V-K Jan 05 '21 at 12:30
  • Nothing exist related to this in log – Irshad Khan Jan 05 '21 at 12:37
  • it's not true. This code logs all sql queries starts from `DB::enableQueryLog();` – V-K Jan 05 '21 at 12:46
  • Where queries exist? in storage/logs folder? – Irshad Khan Jan 05 '21 at 12:49
  • yes. The default file is storage/logs/laravel.log – V-K Jan 05 '21 at 12:50
  • [2020-12-17 08:07:52] local.ERROR: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) (SQL: select * from `users` where `email` = nheathcote@example.org and `users`.`deleted_at` is null limit 1) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 1045): SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) (SQL: select * from `users` where `email` = nheathcote@example.org and `users`.`deleted_at` is null limit 1) at /var/www/html/ereciept/vendor/laravel/framework/src/Illuminate/Database/Connection.php:678) – Irshad Khan Jan 05 '21 at 12:53
  • nope, try to search sql queries with table `invoices` at the end of the file, and also do not add it to comment, edit ur post – V-K Jan 05 '21 at 12:54

0 Answers0