Monday, March 2, 2020

Laravel Group by on single column

In Laravel, when we want to use group by in query builder, it force to use all fields put in group by. For example, select * from employee where employee_id=1001 group by email

Above query will give error:

SQLSTATE[42000]: Syntax error or access violation: 1055 'employee.employee_detail.employee_id' isn't in GROUP BY (SQL: select * from `employee` where (`employee_id` = 1001) group by `email`)

To solve this error,


In your laravel directory, go to the vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php. Copy and search function strictMode() & change function  like below:


protected function strictMode() {
          return "set session
          sql_mode='STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION'";
} 

Follow above steps, Enjoy... 

No comments:

Post a Comment

Copyright © 2019 Tech Location BD. All Right Reserved