Laravel groupBy not working , version 5.4.24 - php

Here is my query, it works on all older versions.
$data = DB::table('applicant')
->groupBy('AppAffID')
->get();
Error i get is :
SQLSTATE[42000]: Syntax error or access violation: 1055 'comloant_loantreeAPI.applicant.AppID' isn't in GROUP BY (SQL: select * from `applicant` group by `AppAffID`)
I have tried the database change :
I added 'strict' => false, to database config.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],

What is it that you want to achieve with the groupBy? If you want to sort on the AppAffID you should use the orderBy instead done like so:
$data = DB::table('applicant')
->orderBy('AppAffID', 'desc') //can also use 'asc' for ascending
->get();

Related

Laravel - Join tables across 2 databases (same server)

I'm trying to join 2 tables in 2 databases. But using the common solutions don't seem to be working and I can't figure out why. Here is what I have:
My connections:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'data'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => env('DB_SCHEMA', 'public'),
],
'auth' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE_AUTH', 'authentication'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => env('DB_SCHEMA', 'public'),
]
The Laravel query I'm trying to run:
Files::where('sender_id',$userId)
->join('authentication.users as users','Policies.sender_id','=','users.id')
->select('*')
->get();
The table Files belong to database data.
But this gives me an error SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "authentication.users" does not exist
I know the connection to the authentication table works since I can do this
DB::connection('auth')->table('users')->whereIn('id', '123')->first();
I've tried replacing authentication with auth but to no avail.
What am I doing wrong? Thanks.
Using query builder :
$query = DB::table('database1.table1 as dt1')->leftjoin('database2.table2 as dt2', 'dt2.ID', '=', 'dt1.ID');
$output = $query->select(['dt1.*','dt2.*'])->get();
using Eloquent model option:
Model1::where('postID',$postID)
->join('database2.table2 as db2','Model1.id','=','db2.id')
->select(['Model1.*','db2.firstName','db2.lastName'])
->orderBy('score','desc')
->get();
This might be a duplicate ask. Pls refer Join two MySQL tables in different databases on the same server with Laravel Eloquent

Why $results = DB::select works while DB::table is not?

I have this mysql driver
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', '{database}'),
'username' => env('DB_USERNAME', '{user}'),
'password' => env('DB_PASSWORD', '{pass}'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
and in my database.php
I have 'default' => env('DB_CONNECTION', 'mysql'),
So When I run in the controller
$results = DB::table('adventureworks2017.sales.store')->get()-first();
It gives me error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`store`' at line 1 (SQL: select * from `adventureworks2017`.`sales`.`store`)
while
$results = DB::select('SELECT * FROM adventureworks2017.`sales.store` limit 5'); it workes
Any reason why query builder is not working wile passing statement works?
You can try passing a raw expression for the table name so the query builder doesn't try to decipher your dot notation into database and table, I suppose.
DB::table(DB::raw('sales.store'))->first();
Or even
DB::table(DB::raw('`sales.store`'))->first();

Laravel issue in join query

here is my code
return DB::table('jobs')->Join('cities','jobs.city_id','=','cities.city_id')
->select('jobs.city_id', DB::raw('COUNT(jobs.city_id) AS num_jobs'))
->groupBy('jobs.city_id')
->orderBy('cities.city')
->limit($limit)
->get();
I want to get result order by alphabetically cities name which is in cities table row name as city and city_id in both tables jobs and cities, I also want number of jobs to show number of jobs in a specific city.
Thanks,
Change strict value to false in database.php file. It will works.
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false, //Change this
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],

Group by not working after upgrading to laravel 5.3

This is the code that was working on laravel 5.2
$menus = CmsMenuItem::groupBy('menu_id')->get();
but now it throws error
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1
of SELECT list is not in GROUP BY clause and contains nonaggregated
column 'convertifier_cms.cms_menu_items.id' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by (SQL: select * from 'cms_menu_items' group
by 'menu_id')
I have also tried
`strict => false`
in database.php but no effect
Try this for database config.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
and use query like this way
$menus =DB::table('cms_menu_item')
->select('*')
->groupBy('menu_id')
->get();
As per this PR just try this in your database config
'strict' => false,
If not there is some known issue is going on.
Please refer these links PR & Issue
Please Go to your config/database.php folder. In mysql configuration array, change strict => true to strict => false, and everything will work nicely.

How to "SET SQL_BIG_SELECTS=1" in Laravel Join statement

when i try to execute DB query with 6 Joins i get following error message:
SQLSTATE[42000]: Syntax error or access violation: 1104 The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
$results = DB::table('table_1')
->join('table_2', 'table_1.id', '=', 'table_2.id')
...
My question is:
How i can set "SET SQL_BIG_SELECTS=1" in laravel query?
i found a solution :)
in app/config/database.php
change following:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
To:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET SESSION SQL_BIG_SELECTS=1',
),
'strict' => false,
],
I added the "options" array and define SQL_BIG_SELECTS
Thats it, we can configure PDO in any way we need!
Hope this is usefull to other ;)

Categories