usmanhalalit / laracsv

CSV files from Eloquent model in seconds - a Laravel package.
MIT License
605 stars 59 forks source link

debugbar conflit #34

Open jonaswebdev opened 5 years ago

jonaswebdev commented 5 years ago

Hi, I just tested this class and love it congratulations to author and all contributors.

I got an issue locally when I created a CSV the laravel-debugbar info are included, see: Screenshot 2019-05-27 21 47 18

When I change my .env to debug=false it's solved :)

usmanhalalit commented 5 years ago

@jonaswebdev I'm not sure what the actual issue is, can you please explain?

projct1 commented 5 years ago

@jonaswebdev Same problem:

        $select = [
            "c.id as ID звонка",
            DB::raw("concat(o.date, ' ', o.time) as 'Дата брони'"),
            "c.created_at as Дата звонка",
            "b.name as Заведение",
            "r.total as Сумма",
            "c.comments as Комменты звонка"
        ];

        $result = DB::table('calls as c')
            ->select($select)
            ->leftJoin('orders as o', 'o.id', 'c.order_id')
            ->leftJoin('bars as b', DB::raw('ifnull(o.bar_id, c.bar_id)'), 'b.id')
            ->leftJoin('revises as r', 'r.order_id', 'o.id')
            ->where('c.is_utm_match', 0)
            ->get();

        $writer = new Export;
        $writer->getWriter()->setDelimiter(';')->addFormatter((new CharsetConverter)->outputEncoding('windows-1251'));
        $writer->build($result, ['ID звонка', 'Дата брони', 'Дата звонка', 'Заведение', 'Сумма', 'Комменты звонка'])->download('метки-не-совпали.csv');

Result is http://skrinshoter.ru/s/080719/gLMDYrtt

usmanhalalit commented 5 years ago

@projct1 I don't get what exactly the error is.

projct1 commented 5 years ago

@usmanhalalit can u see screenshot? http://skrinshoter.ru/s/080719/gLMDYrtt

usmanhalalit commented 5 years ago

@projct1 yes, but I don't understand, I'm not a debugbar user either.

projct1 commented 5 years ago

@usmanhalalit so, that fix problem named "debugbar conflit" should install debugbar i think)

jonaswebdev commented 5 years ago

Hi @usmanhalalit sorry by delay.

So, the issue with debugbar happen on "non-production" environment, it includes some no wished data into CSV file.

I don't know how I could help you, please guide me if you can, but I agree with @projct1, and you could try to use debugbar on your development environment and see the issue with own eyes when you export a CSV file.

thanks in advance

usmanhalalit commented 5 years ago

Thanks for the explanation @jonaswebdev .

I might need to try debugbar.

pikanglong commented 5 years ago

I'm not a debugbar user, but I have the same problem. After running the code below:

        $users = DB::table('users') -> get();
        $csvExporter = new \Laracsv\Export();
        $csvExporter->build($users,['id','name','email','password'])->download();

I found two lines of strange JavaScript code at the end of the csv file downloaded. Here is my screenshot:bugreport.png

My Environment:

By the way, I seem to have encountered some Chinese encoding problems. Is there a function that allows me to customize the export encoding?(utf-8, unicode, ...)

divdax commented 5 years ago

Same problem here. Event i disabled debugbar the CSV contains some javascript code.

projct1 commented 5 years ago

I use native way and plugin becomes no need:

$formatter = new CharsetConverter;
$writer = Writer::createFromFileObject(new SplTempFileObject);
$writer->setDelimiter(';')->addFormatter($formatter->outputEncoding('windows-1251'));

$header = ['ID брони', 'Дата брони', 'Дата звонка', 'Заведение', 'Сумма', 'Комменты звонка'];
$select = ['o.id', DB::raw("concat(o.date, ' ', o.time)"), 'c.created_at', 'b.name', 'r.total', 'c.comments'];
$result = DB::table('calls as c')
    ->select($select)
    ->leftJoin('orders as o', 'o.id', 'c.order_id')
    ->leftJoin('bars as b', DB::raw('ifnull(o.bar_id, c.bar_id)'), 'b.id')
    ->leftJoin('revises as r', 'r.order_id', 'o.id')
    ->where('c.is_utm_match', 0)
    ->get();

$writer->insertOne($header);
$writer->insertAll(json_decode(json_encode($result), true));
$writer->output('метки не совпали.csv');
gianoneil commented 4 years ago

The solution I found was

1) Disable Debugbar on the fly 2) Don't render view after exporting the CSV

if ($export == true) {

    // Disable Debugbar temporarily
    DebugBar::disable();

    // Export CSV
    $csv_exporter = new \Laracsv\Export();
    $csv_exporter->build($orders, ['order_id', 'customer'])->download('orders.csv');

} else {

    // Render the View
    return view('orders.index')->with('orders', $orders);

}
Thowo91 commented 4 years ago

Disable the debugbar work for me but i have to do it with this code in Laravel 5.8 app('debugbar')->disable();

aarondunphy commented 3 years ago

Very late to the party here but what worked for me was adding die(); after the download command. Example:

public function index(Export $export, GetAllSeoCheckDocuments $getAllSeoCheckDocuments)
    {
        $documents = $getAllSeoCheckDocuments->execute();
        $latestSeoResults = $documents->map(fn ($document) => $document->latestSeo);
        $export->build($latestSeoResults, [
            'document_type',
            'document_id',
            'largest_contentful_paint',
        ])->download('seo_test_results_' . date('d_m_Y_h_i_s') . '.csv');
        die();
    }