1

I have a table named shopping_apps, and that table has different entries on different dates. I want to have all records but group all the records on a date basis (not the time part). Suppose I have 20 entries on date1, 12 entries on date2. I want to have only two values extracted from the database: date1 and date2. The controller is below, and Shopping_app is my model name.

<?php

// Use substr() to retrieve date part 10 chars in length.
$shoppingApp = Shopping_app::orderBy('created_at', 'DESC')
    ->groupBy(substr('created_at', 0, 10))
    ->get();

ERROR:

SQLSTATE[42000]: Syntax error or access violation: 1055 'laravelpro4.shopping_apps.id' isn't in GROUP BY (SQL: select * from shopping_apps group by created_at order by created_at desc)

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Vishal Srivastava
  • 542
  • 2
  • 5
  • 19

2 Answers2

1
$shoppingApp = Shopping_app::where('created_at', '>=', \Carbon\Carbon::now->subMonth())
                    ->groupBy(DB::raw('Date(created_at)'))
                    ->orderBy('created_at', 'DESC')->get();

try this maybe it will help you

Manish
  • 118
  • 11
  • ERROR: SQLSTATE[42000]: Syntax error or access violation: 1055 'laravelpro4.shopping_apps.id' isn't in GROUP BY (SQL: select * from `shopping_apps` where `created_at` >= 2019-01-02 12:20:53 group by Date(created_at) order by `created_at` desc) – Vishal Srivastava Feb 02 '19 at 06:51
  • [https://stackoverflow.com/questions/40917189/laravel-syntax-error-or-access-violation-1055-error] Look this for it – Manish Feb 02 '19 at 06:54
1

So with the help of @manish I get the correct answer (what I was trying to get)

Error solved by

Adding false

In config\database.php --> "mysql" array Set 'strict' => false to disable all.

Final code :

$shoppingApp = Shopping_app::where('created_at', '>=', \Carbon\Carbon::now()->subMonth())
                    ->groupBy(DB::raw('Date(created_at)'))
                    ->orderBy('created_at', 'DESC')->get();
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Vishal Srivastava
  • 542
  • 2
  • 5
  • 19