plcosta's avatar

Lumen duplicate values?

I'm using Lumen to make a simple API. I have the following action in my controller:

  public function index()
  {
    $beds = Bed::all();
    return response()->json($beds);
  }

This is the json result:

[
{
    "0": 2,
    "1": 2000001,
    "2": 3,
    "3": "2016-02-02 21:43:58",
    "4": "2016-02-02 21:43:58",
    "id": 2,
    "bed_id": 2000001,
    "status_id": 3,
    "created_at": "2016-02-02 21:43:58",
    "updated_at": "2016-02-02 21:43:58"
},
{
    "0": 3,
    "1": 2000002,
    "2": 4,
    "3": "2016-02-02 21:43:58",
    "4": "2016-02-02 21:43:58",
    "id": 3,
    "bed_id": 2000002,
    "status_id": 4,
    "created_at": "2016-02-02 21:43:58",
    "updated_at": "2016-02-02 21:43:58"
}
]

I have no idea why the results are duplicates. Does anyone have?

0 likes
5 replies
jekinney's avatar

I see two different id's and two different bed_ids.

Also Laravel/lumen returns Json with out the need to specify it.

Return Bed::all() is the same as you have.

plcosta's avatar

@jekinney the problem isn't two results. It is the results each have columns and then a copy on the data as numeric properties.

jekinney's avatar

@plcosta

Got yeah,

try

public function index()
  {
    return Bed::all(); // by default it returns JSON, I have never had the issue as you stated, just a JSON object as expected
  }
plcosta's avatar

@jekinney this problem occurs when I use two connections. I created a config directory in my project and add the file config/database.php with two connections. When I remove this file, that works fine.

jekinney's avatar

@plcosta

Two Database connections? If so you can set the connection in a couple of ways. I personally set it in the model

protected $connection = 'connection_name_as_set_in_database.php';

That specifically sets the connection to which database you want. Weird output on that then if that is the case with two database connections.

Please or to participate in this conversation.