2

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    https://stackoverflow.com/questions/31527050/laravel-5-controller-sending-json-integer-as-string might be of some help – aynber Apr 26 '21 at 16:29

2 Answers2

0

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.

linuxartisan
  • 2,396
  • 3
  • 21
  • 40
0

You can use casting Attribute Casting in your model

protected $casts = [ 
        'field_name1' => 'integer', 
    ];
Dhairya Lakhera
  • 4,445
  • 3
  • 35
  • 62