2

I have a mysql query:

SELECT MAX(CAST(REPLACE(name,'test','') AS SIGNED)) FROM `accts` WHERE `name` LIKE 'test%';

If I run the query in the MySQL console it executes.

If I execute this query with mysqli_query in PHP

mysqli_query($connection, "SELECT max(cast(replace(name,'test','') as signed)) FROM accts WHERE name LIKE '$test%'")

it executes.

If I run it in laravel:

DB::connection('db')
                ->table('accts')
                ->select(DB::raw("max(cast(replace(name,'test','') as signed))"))
                ->where('name', 'like',  "'$test%'")
                ->get();

It throws an error:

SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(), MAX(), COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select max(cast(replace(name,'test','') as signed)) from `accts` where `name` like 'test%')

Does anybody have any idea what is happening? The error looks like it is coming from the server but the server has no problem with the query. Is there an issue with how laravel is executing the query?

ZCT
  • 181
  • 1
  • 1
  • 13

1 Answers1

3

Your problem must be related to database configuration,

In your config/database.php turn off the strict mode ('strict' => false,).

Example:

'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,
    ],

References:

Laravel Eloquent Where

Syntax error or access violation: 1140 Mixing of GROUP columns laravel

Bulfaitelo
  • 515
  • 7
  • 18