How to get the correct data type from MYSQL in Laravel?

0 like 0 dislike
6 views
Hello.
Working with Laravel 5.6

In MYSQL database there is a column of type int
But when I get in any way the data from the database, be it a model or a direct request
I always returned all the data from the database in string format and this makes very difficult the development process, as you have everywhere to translate manually in Integer

Can setting PDO what to ask to connect ?
by | 6 views

4 Answers

0 like 0 dislike
Like discussed here. Check that you have the normal version of php and mysql.
by
0 like 0 dislike
https://github.com/laravel/framework/blob/1eb1314d...

If correct on true - get the ID as a string.
PDO::ATTR_STRINGIFY_FETCHES => true,
PDO::ATTR_EMULATE_PREPARES => true,

If you are using pure PDO try to specify these options.
by
0 like 0 dislike
For models https://laravel.com/docs/5.6/eloquent-mutators#att...
For normal DB requests and really need to think of something Why pdo driver returns int as a string?
by
0 like 0 dislike
On SO there is a solution, but it looks like something non-standard, IMHO easier to add the dB class when you return from the select test type and return already prepared, something like:
if($row = $sql->fetch(PDO::FETCH_ASSOC) !== false){ foreach($row as $k=>$v) { if(is_numeric($v)) $row[$k] = intval($v); } }


It is also a crutch, but at least it won't have each query, all wrapped in a check.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,665 users