phplaravellaravel-5postman

Why integer data is returned as string in Postman


I'm using Postman to test my APIs and I get the following response:

{
 "Message": "Drinks loaded successfully.",
 "Status": true,
 "InnerData": [
     {
         "id": 1,
         "place_id": "25",
         "drink_type": "1",
         "drink_amount": "2",
         "device_id": "1",
         "created_at": "2018-03-09 14:22:27",
         "updated_at": "2018-03-09 14:22:27"
     }
 ]
}

In my datbase the drink_type, place_id, drink_amount, device_id are all integers but I receive them as strings, what is wrong with them?

Here is my code used to return this data:

public function loadDrinks(Request $request){
    $place_id = $request->place_id;
    $device_id = $request->device_id;

    $drinks = \App\DrinksOrdered::where(['place_id' => $place_id, 'device_id' => $device_id])->get();
    $resp = new \App\Http\Helpers\ServiceResponse;
    $resp->Message = "Drinks loaded successfully.";
    $resp->Status = true;
    $resp->InnerData = $drinks;
    return response()->json($resp, 200);
}

Solution

  • https://laravel.com/docs/5.6/eloquent-mutators#attribute-casting

    Add this to your model to specify the data type:

     protected $casts = [
         'drink_type' => 'int',
         'place_id' => 'int'
     ];