{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/7507","labels_url":"https://api.github.com/repos/laravel/framework/issues/7507/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/7507/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/7507/events","html_url":"https://github.com/laravel/framework/pull/7507","id":58220817,"number":7507,"title":"Casting nullable fields","user":{"login":"rwillians","id":5093551,"avatar_url":"https://avatars.githubusercontent.com/u/5093551?v=3","gravatar_id":"","url":"https://api.github.com/users/rwillians","html_url":"https://github.com/rwillians","followers_url":"https://api.github.com/users/rwillians/followers","following_url":"https://api.github.com/users/rwillians/following{/other_user}","gists_url":"https://api.github.com/users/rwillians/gists{/gist_id}","starred_url":"https://api.github.com/users/rwillians/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwillians/subscriptions","organizations_url":"https://api.github.com/users/rwillians/orgs","repos_url":"https://api.github.com/users/rwillians/repos","events_url":"https://api.github.com/users/rwillians/events{/privacy}","received_events_url":"https://api.github.com/users/rwillians/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/laravel/framework/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/laravel/framework/labels/database","name":"database","color":"eb6420"},{"url":"https://api.github.com/repos/laravel/framework/labels/medium%20priority","name":"medium priority","color":"fad8c7"},{"url":"https://api.github.com/repos/laravel/framework/labels/proposal","name":"proposal","color":"d4c5f9"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-02-19T15:14:23Z","updated_at":"2015-02-19T16:02:45Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/laravel/framework/pulls/7507","html_url":"https://github.com/laravel/framework/pull/7507","diff_url":"https://github.com/laravel/framework/pull/7507.diff","patch_url":"https://github.com/laravel/framework/pull/7507.patch"},"body":"Perhaps casting types should support nullable fields in order not to change the expected output value.\r\n\r\n## Issue\r\n- Casting nullable fields to ```integer```, ```real```, ```float``` or ```double``` changes the output value to 0 (zero);\r\n- Casting nullable fields to ```string```changes the output value to \"\" (empty).\r\n\r\n## Scenario\r\nConsider you have the model below that is a representation of a mobile phone, where the ```null``` \"carrier_id\" represents there is a job pending to find the phone's carrier.\r\n```json\r\n{\r\n    \"id\": 1,\r\n    \"carrier_id\": null,\r\n    \"country_id\": 55,\r\n    \"area\": 11,\r\n    \"number\": 987654321\r\n}\r\n```\r\n\r\nUsing the following casts:\r\n```php\r\nprotected $casts = [\r\n    'id' => 'integer',\r\n    'carrier_id' => 'integer',\r\n    'country_id' => 'integer',\r\n    'area' => 'integer',\r\n    'number' => 'integer'\r\n];\r\n```\r\n\r\nWhen you output your model (without the suggested changes) you will get:\r\n```json\r\n{\r\n    \"id\": 1,\r\n    \"carrier_id\": 0,\r\n    \"country_id\": 55,\r\n    \"area\": 11,\r\n    \"number\": 987654321\r\n}\r\n```\r\n\r\nWith the suggested changes you will get the correct expected output:\r\n```json\r\n{\r\n    \"id\": 1,\r\n    \"carrier_id\": null,\r\n    \"country_id\": 55,\r\n    \"area\": 11,\r\n    \"number\": 987654321\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/75078008","html_url":"https://github.com/laravel/framework/pull/7507#issuecomment-75078008","issue_url":"https://api.github.com/repos/laravel/framework/issues/7507","id":75078008,"user":{"login":"Arrilot","id":2826480,"avatar_url":"https://avatars.githubusercontent.com/u/2826480?v=3","gravatar_id":"","url":"https://api.github.com/users/Arrilot","html_url":"https://github.com/Arrilot","followers_url":"https://api.github.com/users/Arrilot/followers","following_url":"https://api.github.com/users/Arrilot/following{/other_user}","gists_url":"https://api.github.com/users/Arrilot/gists{/gist_id}","starred_url":"https://api.github.com/users/Arrilot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Arrilot/subscriptions","organizations_url":"https://api.github.com/users/Arrilot/orgs","repos_url":"https://api.github.com/users/Arrilot/repos","events_url":"https://api.github.com/users/Arrilot/events{/privacy}","received_events_url":"https://api.github.com/users/Arrilot/received_events","type":"User","site_admin":false},"created_at":"2015-02-19T16:02:45Z","updated_at":"2015-02-19T16:02:45Z","body":"@barryvdh Yup, even better :+1: "}}