CSV导出不会按正确的顺序显示group_concat值

时间:2017-04-09 14:04:45

标签: php mysql laravel csv

我使用group concat从mysql db中检索我的数据。当我将那些valuse导出到csv coloumns时,使用group_concat并没有显示正确的顺序。请告诉我出错的地方

     $data = DB::table('festivals')
        ->leftjoin('namedesgs', 'festivals.id', '=', 'namedesgs.festival')
         ->leftjoin('vehicles', 'festivals.id', '=', 'vehicles.festival')
        ->select(DB::raw(" GROUP_CONCAT( DISTINCT vehicles.role SEPARATOR '\n') as role,GROUP_CONCAT( DISTINCT vehicles.size SEPARATOR '\n') as size,festivals.id,festivals.ref_no as ref_no, festivals.camping,festivals.tour_mgr_name,festivals.email,festivals.mobile,festivals.name_address, GROUP_CONCAT(DISTINCT namedesgs.name SEPARATOR '\n') as names,GROUP_CONCAT(DISTINCT namedesgs.designation SEPARATOR '\n') as designations"))
        ->groupBy('festivals.id')
        ->get();


$filename = "students.csv";
$handle = fopen($filename, 'w+');

fputcsv($handle, array('ref_no','camping','tour_mgr_name','email','mobile','name_address','names', 'designations','role', 'size'));
foreach ($data as $student)
{
    fputcsv($handle, array(
              $student->ref_no,
              $student->camping, 
              $student->tour_mgr_name,
              $student->email,
              $student->mobile,
              $student->name_address,
              $student->names,
              $student->designations,
              $student->role,
              $student->size,

         ));
}
fclose($handle);

$headers = array(
    'Content-Type' => 'text/csv',
);

return \Response::download($filename, '2017.csv', $headers);
下面的

是我输出的屏幕截图

enter image description here

ex:role1,role2角色3混乱。

0 个答案:

没有答案