Problem in recovering data in a large application

2

My table is constituted as follows:

╔════════════════════════╗
║        USER            ║
╠═══════════╦════════════╣
║ Id        ║ Integer    ║
║ Name      ║ String     ║
║ Address   ║ String     ║
╚═══════════╩════════════╝

I have some data already stored in the database, exp:

╔═══════════╦═══════════╦═══════════════╗
║ Id        ║ Name      ║ Address       ║
╠═══════════╬═══════════╬═══════════════╣
║ 1         ║ Marcelo   ║ Rua A         ║
║ 2         ║ Ferdinand ║ Rua B         ║
║ 3         ║ Marcelo   ║ Rua A         ║
╚═══════════╩═══════════╩═══════════════╝

In my controller I have the following method:

public function getListUsers()
{
    $users = DB::table("users")->pluck('Name','Address');
    echo json_encode($LatLng);
    return;
    // return response()->json($users);
}

When I display the route that invokes the controller function, it displays the following array:

  

{"Marcelo": "Street A", "Ferdinand": "Street B"}

I tried to run the following:

    DB::enableQueryLog();
    $users= DB::table("users")
                ->pluck("name","address");
    print_r(
        DB::getQueryLog()
    );

In the log, the select executed is as follows:

 select 'user', 'address' from 'user'

And if I run the same select in my database, it brings me the correct data, which should be something like:

  

[{"name": "Marcelo", "Address": "Street A"}, {"name": "Ferdinand", "Address" , "Address": "Street A"}]

    
asked by anonymous 12.11.2017 / 22:59

1 answer

3

You are using the wrong function. After executing the query in the database, the pluck function will transform your result as an array where the first field is the value and the second is the key .

$collection = collect([
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
]);

$plucked = $collection->pluck('name');

$plucked->all();

// ['Desk', 'Chair']

For what you want to do, use select() and get()

$users = DB::table("users")->select('Name','Address')->get();

return response()->json($users);
    
12.11.2017 / 23:22