laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 419 forks source link

Results are duplicated by column numbers #375

Closed plcosta closed 8 years ago

plcosta commented 8 years ago

Hi there!

I am build a simple query using Eloquent. 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"
  }
]

The problem is not directly in this query, but any response has duplicates columns (as numbers).

Any thoughts?

GrahamCampbell commented 8 years ago

no bug

GrahamCampbell commented 8 years ago

you actually have two rows

Garbee commented 8 years ago

Can you provide a minimal sample that reproduces the error? I'm not seeing this happen here.

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

plcosta commented 8 years ago

@Garbee Please take a look at: https://gist.github.com/plcosta/d3586ed721b6022906a7

@GrahamCampbell like Garbee said, the problem isn't two results. It is the results each have columns and then a copy on the data as numeric properties. The problem is not directly in this query, but any response has duplicates columns (as numbers).

lumen-duplicate

I'm using 5.2.4 release.

Can you reopen this issue?

GrahamCampbell commented 8 years ago

Please open the issue on laravel since eloquent has nothing to do with lumen.

plcosta commented 8 years ago

@GrahamCampbell it occurs with Lumen and Eloquent only. But with Laravel and Eloquent have no problem. I'll try there.

plcosta commented 8 years ago

I tested now and the problem persists without Eloquent.

Garbee commented 8 years ago

I'm unable to replicate the issue here. Perhaps there is something going on in your configuration causing this.

Without a vagrant box or docker container to run that replicates the issue, it appears nothing is wrong in the framework.

plcosta commented 8 years ago

@Garbee Thanks! I'll test.

plcosta commented 8 years ago

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

plcosta commented 8 years ago

Finally the problem was solved.

When the database.php file is created, you need to add the fetch param, like this:

<?php

return [

    'fetch' => PDO::FETCH_CLASS,
...
]
Garbee commented 8 years ago

@plcosta The fetch parameter is already set to that by default. I would be surprised if that is the actual cause of the issue.

plcosta commented 8 years ago

@Garbee Apparently, when you create the database.php file, the default file is overwritten. So, you need to copy the entire file. Please, make a test. Create a config directory in your project and add the file config/database.php without the fetch parameter.

MrigKush commented 8 years ago

I am also facing same issue. but thanks to @plcosta for his solution. Issue Resolved

gokigoks commented 8 years ago

@plcosta thanks alot brother! saved us a few hours of digging.

PrinceDavis commented 7 years ago

thanks bro @plcosta

CammoKing commented 7 years ago

Very helpful, thank you so much!

shyMang0 commented 5 years ago

just use $rows = $pdo->fetchAll(\PDO::FETCH_ASSOC);