Framework: Eloquent attribute: size limited to 1 megabyte?

Created on 20 May 2013  路  11Comments  路  Source: laravel/framework

I have a mediumtext column in my database. When I fetch its string value (with Eloquent), it's truncated to size 1048576 (1 megabyte). Is there a turnaround? Thanks

Most helpful comment

@geomagilles's solution works great unless you're using mysqlnd. You'll get an error saying Undefined class constant 'MYSQL_ATTR_MAX_BUFFER_SIZE', because, as the PDO documentation points out, "this constant is not supported when compiled against mysqlnd."

So if your application needs to run on multiple machines, or if you want to be defensive for some other reason, you'll need to check the existence of the constant before using it.

Note that you cannot set this attribute through PDO::setAttribute, so you can't do this in a service provider after the PDO has already been created. You'll have to add the conditional to the connection's configuration in config/database.php, e.g.:

    'options'   =>
        defined('PDO::MYSQL_ATTR_MAX_BUFFER_SIZE')
            ? [ PDO::MYSQL_ATTR_MAX_BUFFER_SIZE => 16 * 1024 * 1024 ]
            : [],

All 11 comments

Actually, on my local server I do not have this issue. How this could be a problem with my production server configuration?

Bug was fixed by adding
'options' => array(PDO::MYSQL_ATTR_MAX_BUFFER_SIZE => 16777216),
on config (database.connections mysql)

It occurs with Database driver too. However for me it didnt fix the problem

Just had this problem, thank you for posting the fix @geomagilles

I was storing JSON in a longtext column, and for some strange reason json_decode wasn't able to decode data it had encoded previously. Turns out it had hit this 1mb limit, so wasn't getting the contents of the field.

Running PHP 5.4.29 in production, but PHP 5.5.13 locally.

The PHP version doesn't seen to be the issue here. It seems to be using the mysql driver instead of the preferred mysqlnd (native driver). There is a bug report in PHP that states mysqlnd allocates as much memory as needed, where the mysql driver does not.

Hey Guys
I just spend 2 days on this problem on my Virtual Machine with Laravel. When suddenly the light goes on thanks to this post....
And on my local machine there is no problem.
You give me a huge indication guys
However I did not catch @Garbee. How explaining why on prod server there is problem and no problem on dev ?
and really thanks @geomagilles for the fix

Had the same issue, and the same headache :)
The solution provided by @geomagilles worked very well. Thanks a lot !

@geomagilles's solution works great unless you're using mysqlnd. You'll get an error saying Undefined class constant 'MYSQL_ATTR_MAX_BUFFER_SIZE', because, as the PDO documentation points out, "this constant is not supported when compiled against mysqlnd."

So if your application needs to run on multiple machines, or if you want to be defensive for some other reason, you'll need to check the existence of the constant before using it.

Note that you cannot set this attribute through PDO::setAttribute, so you can't do this in a service provider after the PDO has already been created. You'll have to add the conditional to the connection's configuration in config/database.php, e.g.:

    'options'   =>
        defined('PDO::MYSQL_ATTR_MAX_BUFFER_SIZE')
            ? [ PDO::MYSQL_ATTR_MAX_BUFFER_SIZE => 16 * 1024 * 1024 ]
            : [],

hi...
mysql native driver is enable in my case...
what should i do?
can i disable it?how?

I'm having the same issue and i saw that as of mysql 5.6.2, there is the variable innodb_change_buffer_max_size...is this the native driver equivalent? coz' so far it's still not working. Any help will be appreciated. Thanks!

Yeahhh, finaly found above :D

The solution: "PDO::MYSQL_ATTR_MAX_BUFFER_SIZE => 16777216"

My reason: Laravel 4 -> on xampp and php 7.3.6 works corretly. But in production, php 7.3.20 it doesn't. Long data in one column fetched via PDO query builder was truncated to 1048576 chars/bytes.

My configuration after correction - using mySql in config/database.php:

'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,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
PDO::MYSQL_ATTR_MAX_BUFFER_SIZE => 16777216
]) : [],
],

Hope it helps to somebody.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixsanz picture felixsanz  路  3Comments

JamborJan picture JamborJan  路  3Comments

kerbylav picture kerbylav  路  3Comments

shopblocks picture shopblocks  路  3Comments

PhiloNL picture PhiloNL  路  3Comments