dilfdo
3 years ago

Laravel 5 download csv with leading zeros

Posted 3 years ago by dilfdo

when I generate my csv file , some of the special charactors and front zero of cell values are not getting . pls advice how to fix this

    {
        // $table = Festivals::all();
        $headers = [
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
            , 'Content-type' => 'text/csv'
            , 'Content-Disposition' => 'attachment; filename=sss.csv'
            , 'Expires' => '0'
            , 'Pragma' => 'public'
        ];

              $list = Festivals::select('ref_no','artist_name','postal_address','payment','payment_fee','bank_details','bank_name','bank_address','account_name','account_number','sort_code','reg_vat','vat_reg_number'
,'foreign_artise','name_desg','email','mobile','name_address','vehicle_size','backline','additional_backline','name','date')->get()->toArray();


        # add headers for each column in the CSV download
        array_unshift($list, array_keys($list[0]));

        $callback = function () use ($list) {
            $FH = fopen('php://output', 'w');
            foreach ($list as $row) {
                fputcsv($FH, $row);
            }
            fclose($FH);
        };

        return Response::stream($callback, 200, $headers);
    }
 

Please sign in or create an account to participate in this conversation.