I would like to return INT as integers from my database. At the moment all values are loaded as Strings. Database is MSSQL 2012 and I use the PDO driver (for v5.6).
Trying to set the properties here (as shown here on fideloper.com, but I don't know if that's still possible):
'sqlsrv' => [
'driver' => 'sqlsrv',
'charset' => 'utf8',
'prefix' => '',
......
'options' => array(
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
),
],
But always getting an error:
SQLSTATE[IMSSP]: The given attribute is only supported on the PDOStatement object.
How can I set any settings for the PDO Driver to return INT as Integers and not as Strings.
This is still not working:
$pdo = DB::connection()->getPdo();
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
.. do ORM Query
Bringing the same error.
Maybe anyone can help me?
PHP's PDO has many attribute options you can configure. Occasionally, we may need to change some options to fit our infrastructure or code needs. For example, we might need to turn on persistent connections.
Echo the Laravel database name in Blade/PHP This will output the name of the database or return 'none' if there is no connection. If you view it in the browser, it gives you the name of the connected database. Checking whether the application is connected to a Laravel database.
A database transaction is a set of operations that you can carry out securely within the database structure of your application, such as SQL queries to modify data (e.g. updates, deletions, and insertions). At any point, you can decide to roll back all the transaction's queries.
Laravel is one of the top php mvc framework. PDO is oops based and more securable then mysql and mysqli.
I know this is an old thread, but I found a global solution to this issue for MSSQL drivers and PHP 7 (single change that affects all tables / models). Hopefully this will help others that are struggling with the same.
php/ext
folder (replacing/deleting the older version). You'll probably need to stop/start your web server (definitely, if IIS) to free the original files up for replacement/deletion. If the filenames changed from the previous version you had installed, update your php.ini
file.
Update the driver configuration to include the new PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE
parameter:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'laravel'),
'password' => env('DB_PASSWORD', 'password#1'),
'charset' => 'utf8',
'prefix' => '',
'options' => array(
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true
),
],
For explanation of the solution, I found it by working my way through the source code on the Git Repository. Of course it would have been a lot easier if I had read the Announcements section of the README file first:
July 28, 2016 (4.1.0): Thanks to the community's input, this release expands drivers functionalities and also includes some bug fixes:
SQLSRV_ATTR_FETCHES_NUMERIC_TYPE
connection attribute flag is added to PDO_SQLSRV driver to handle numeric fetches from columns with numeric Sql types (only bit, integer, smallint, tinyint, float and real). This flag can be turned on by setting its value inPDO::setAttribute
totrue
, For example,$conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE,true);
IfSQLSRV_ATTR_FETCHES_NUMERIC_TYPE
is set totrue
the results from an integer column will be represented as anint
, likewise, Sql types float and real will be represented asfloat
. Note for exceptions:
- When connection option flag
ATTR_STRINGIFY_FETCHES
is on, even whenSQLSRV_ATTR_FETCHES_NUMERIC_TYPE
is on, the return value will still be string.- When the returned PDO type in bind column is
PDO_PARAM_INT
, the return value from a integer column will be int even ifSQLSRV_ATTR_FETCHES_NUMERIC_TYPE
is off.
I believe this issue is related to the PDO driver used (thats installed with PHP, not laravel configuration).
Not quite what you're looking for but could potentially solve your problems. Since laravel 5 theres been a casts feature on eloquent where your columns are automatically cast to your pre-defined types. See http://laravel.com/docs/5.0/eloquent#attribute-casting
// Eloquent Model
protected $casts = [
'int_column' => 'int',
];
Your int_column
would then automatically be cast to an int when the model is retrieved from the database
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With