Issue with Laravel casting Int to string when fetching from server?

I have my web application running on server. I tested the code in local and it works fine. I’m getting id as int from local server but string in hosted server.

Using PHP 7.3

$data = DB::table('users')
->select('id')
->get();

$data = $data->pluck(id);

(Local)
output => [1,2,3]

(Hosted)
output => [“1″,”2″,”3”]

Can any one help to resolve this issue? I can do loop to type cast but there are 100’s for function in my controller. It is not possible to type cast each query.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use casting Attribute Casting in your model

protected $casts = [ 
        'field_name1' => 'integer', 
    ];

Method 2

This is due to a missing plugin on the production server (hosting server).
You need to install the mysqlnd plugin to fix the issue.

You have not mentioned which OS you are using. If it’s Linux, you can check whether mysqlnd is installed like so

$ sudo dpkg -l | grep 'mysqlnd'

If it’s not installed, you need to install it like so (assuming you have php5)

$ sudo apt-get install php-mysqlnd

Package name might differ based on the PHP version.

Refer my answer for more information.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x