I'm a new, inexperienced Laravel developer and I'm confused by the query results I'm getting. I'm trying to write to results of a query to a CSV file. My code:
$connection = fopen('/BSTablespace/someFile.txt', 'w');
$users = DB::table('users')->select('first_name', 'agent_id_from_website', 'sales_manager_id', 'last_name')->toSql();
$generator = function () use ($users){
$db = DB::connection()->getPdo();
$query = $db->prepare($users);
$query->execute();
while($user = $query->fetch()){
yield($user);
}
};
foreach ($generator() as $g){
fputcsv($connection, array_values($g));
}
fclose($connection);
Instead of getting one result per column, I get 2. One keyed with the column name and one keyed with a numeric index, like so:
'first_name' => 'Jane', 0 => 'Jane', 'agent_id_from_website' => '450', 1 => '450', 'sales_manager_id' => 317, 2 => 317, 'last_name' => 'Doe', 3 => 'Doe'
Why am I getting duplicate results? Obviously I can just ignore the duplicates but I'm very curious as to why PHP, Laravel, or Postgres handles it this way.