40

On my development server the JSON response from the Laravel 5 controller shows the data in the correct types.

e.g

imdb_rating: 7.6
imdb_votes: 6271

But on the production server, the JSON response is sent back as strings.

imdb_rating: "7.60"
imdb_votes: "6271"

Both development and production have the same version of PHP installed (5.6.11-1).

Any ideas on what may be causing this behaviour?

Muggles
  • 1,526
  • 3
  • 14
  • 21

5 Answers5

73

I just ran into this same issue! For those of you looking for a more appropriate solution, you might want to check out the $casts property for Eloquent models.

The accepted solution will work, but it will also convert fields that you may not want converted. I would recommend adding this to your Eloquent model:

protected $casts = [ 'imdb_rating' => 'float', 'imdb_votes' => 'integer' ];

This will convert the values directly in your model object, so you won't need to worry about updating multiple endpoints. I hope this helps others as it did me!

davidmyersdev
  • 1,042
  • 8
  • 10
  • Excellent solution. I like that this solves the issue regardless of underlying platform. Here's a quick link to the docs. Great stuff here: https://laravel.com/docs/5.4/eloquent-mutators#attribute-casting – TomWilsonFL Feb 03 '17 at 17:51
  • 2
    Annoyed that laravel requires this overhead to fix the data types. How does this affect performance? – zeros-and-ones Aug 15 '17 at 05:33
  • 6
    It worked! But I still don't quite understand when my DB datatype is tinyint why do I need to manually cast it to an int? Ridiculous! – kiradotee Nov 01 '17 at 15:40
  • 1
    This must be the accepted answer I agree. I nearly followed the accepted answer and would have regretted the mess it could have caused in my code. – Tim Head Jul 06 '18 at 15:11
  • Why does this not working for me and the other one is? I am casting ` 'latitude' => 'double' `, it still sends as string. – Mike Ross Oct 11 '19 at 04:57
  • Thanks, this is very useful – Mohammed Riyadh Aug 10 '20 at 09:51
58

Make sure to use MySQL Native Driver which will support native data types.

It is possible to convert integer and float columns back to PHP numbers by setting the MYSQLI_OPT_INT_AND_FLOAT_NATIVE connection option, if using the mysqlnd library. If set, the mysqlnd library will check the result set meta data column types and convert numeric SQL columns to PHP numbers, if the PHP data type value range allows for it. This way, for example, SQL INT columns are returned as integers.

MySQL Client Library will return all fields as strings.

Another solution would be to use json_encode options with JSON_NUMERIC_CHECK.

For example:

return response()->json(["foo" => "bar"], 200, [], JSON_NUMERIC_CHECK);
DavidDomain
  • 14,976
  • 4
  • 42
  • 50
  • 5
    this finally solve my problem, the installation process is quite simple in ubuntu: `apt-get remove php5-mysql`,`apt-get install php5-mysqlnd`, `service apache2 restart` – Alejandro Silva Jul 19 '16 at 15:19
  • 1
    Thanks. =D. Works con CentOS via installing php55w-mysqlnd – Esselans Dec 16 '16 at 16:22
16

Same issue happened to me too, Laravel was returning all numerical data as string after I published to a shared hosting. But everything is working fine on my localhost. So found this solution.

Goto cPanel --> Select PHP Version --> Extensions tab

then uncheck pdo_mysql and check nd_pdo_mysql extension. See following screenshot of my cPanel extensions list cPanel extensions list

I found this solution by comparing phpInfo of my localhost and shared hosting server. see following 2 screenshots of phpInfo page before and after I made the change.

Before the fix Before the fix

After the fix enter image description here

Thanks for the answer by DavidDomain and it opens my eyes to find this solution

I hope my answer will help to someone.

mili
  • 3,502
  • 1
  • 29
  • 29
0

you can return the integer with typecasting like

return (int)Post::find(1)->id;
MH Fuad
  • 746
  • 6
  • 16
0

I think this can help too in order to make it easier! I'm using laravel v9.x.

Put the function in BaseController:

class Controller extends BaseController {
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public static function json($json) {
        return response()->json($json, 200, [], JSON_NUMERIC_CHECK);
    }

}

Now in your model or whatever you use:

return self::json($all);
Hadi Note
  • 1,386
  • 17
  • 16