LoginSignup
0
2

More than 5 years have passed since last update.

Laravel DBのint型のカラムがstring型で取得される

Last updated at Posted at 2019-04-10

ここ2、3日ハマっていた「DBのint型のカラムがstring型で取得される」現象の原因がわかりました。

現象

int型のカラムの取得がstringになってしまう。

原因

解決方法

config/database.php のsqlsrvの記述に、optionsを追加。

'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', ''),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'options' => array(
                PDO::ATTR_STRINGIFY_FETCHES => false,
                PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true
            )
        ],
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2