itguy614
1 month ago
146
8
General

toJson() converting 0 to null

Posted 1 month ago by itguy614

I have a model that is returning the following results.

=> App\Models\EnvironmentPolicy {#3891
     id: 3,
     name: "test",
     critical_low_temp: 0.0,
     warning_low_temp: 0.0,
     warning_high_temp: 0.0,
     critical_high_temp: 0.0,
     critical_low_humidity: null,
     warning_low_humidity: null,
     warning_high_humidity: null,
     critical_high_humidity: null,
     read_frequency: 900,
     warning_read_frequency: 900,
     critical_read_frequency: 900,
     modifiable: 1,
     created_at: "2020-01-13 21:38:17",
     updated_at: "2020-01-13 21:38:17",
     deleted_at: null,
   }

->toJson() output. json_encode has the same behavior.

=> "   {\n
       "id": 3,\n
       "name": "test",\n
       "critical_low_temp": null,\n
       "warning_low_temp": null,\n
       "warning_high_temp": null,\n
       "critical_high_temp": null,\n
       "critical_low_humidity": null,\n
       "warning_low_humidity": null,\n
       "warning_high_humidity": null,\n
       "critical_high_humidity": null,\n
       "read_frequency": 15,\n
       "warning_read_frequency": 15,\n
       "critical_read_frequency": 15,\n
       "modifiable": true,\n
       "created_at": "2020-01-13 21:38:17",\n
       "updated_at": "2020-01-13 21:38:17",\n
       "deleted_at": null,\n
       "temp_entity": "℃",\n
       "temp_unit": "C",\n
       "local_created_at": "2020-01-13 16:38:17",\n
       "formatted_created_at": "2020-01-13 16:38:17",\n
       "local_updated_at": "2020-01-13 16:38:17",\n
       "formatted_updated_at": "2020-01-13 16:38:17"\n
   }

In my case, the difference between 0 and null for the temperature points means something. When I take this model and convert to Json, the 0 is replaced by null. I'm looking for a way to change this behavior.

Please sign in or create an account to participate in this conversation.