Hi all, i have a very strange problem.
I'm joining tables with this query:
$studentsCount = DB::table('cities as c')
->select('c.id', 'c.lat', 'c.lng', 'ct.name', 'ct.slug', DB::raw('count(sj.student_id) as student_count'))
->join('city_translations as ct', 'ct.city_id', '=', 'c.id')
->join('companies as comp', 'comp.city_id', '=', 'c.id')
->join('student_jobs as sj', 'sj.company_id', '=', 'comp.id')
->join('students as s', 's.id', '=', 'sj.student_id')
->where('ct.locale', $locale)
->where('s.active', 1)
->groupBy('c.id')
->get();
When I run that query from MySQL workbench it works fine no aggregate error. Also tried to run it from cli on Homestead where MySQL workbench is connecting.
I got the real sql with (without ->get()):
$studentsCount->toSql()
The aggregate error appears when I use that query in laravel:
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'alumni.ct.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `c`.`id`, `c`.`lat`, `c`.`lng`, `ct`.`name`, `ct`.`slug`, count(sj.student_id) as student_count from `cities` as `c` inner join `city_translations` as `ct` on `ct`.`city_id` = `c`.`id` inner join `companies` as `comp` on `comp`.`city_id` = `c`.`id` inner join `student_jobs` as `sj` on `sj`.`company_id` = `comp`.`id` inner join `students` as `s` on `s`.`id` = `sj`.`student_id` where `ct`.`locale` = sr and `s`.`active` = 1 group by `c`.`id`) in file /home/vagrant/projects/vts-alumni/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664
Stack trace:
When I add the nonaggregated fields to the query this way:
->groupBy('c.id', 'ct.name', 'ct.slug')
It works.
I don't understand how the error is not appearing from mysql cli and workbench but appearing from laravel.
Laravel is connecting to the same mysql server where I run my tests from cli and workbench.