Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't work export with FromQuery, WithMapping, WithHeadings, ShouldQueue #1833

Closed
3 tasks
PopovMaxim opened this issue Oct 14, 2018 · 6 comments
Closed
3 tasks

Comments

@PopovMaxim
Copy link

PopovMaxim commented Oct 14, 2018

  • Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • Checked that your issue isn't already filed.
  • Checked if no PR was submitted that fixes this problem.

Versions

  • PHP version: 7.2
  • Laravel version: 5.7
  • Package version: 3.1

Description

Hi. I'm trying to export a large number of rows in the background. This task works, but as soon as I need to add headings to the columns, it does not work for me. Rows are exported from the table as is. I did everything according to the instructions in the documentation.

Steps to Reproduce

Additional Information

My Exporter Class

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Illuminate\Contracts\Queue\ShouldQueue;

class TransactionsExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, ShouldQueue
{
    use Exportable;

    public $merchant_ids = [];

    public function __construct($merchant_ids) {
        $this->merchant_ids = $merchant_ids;
    }

    public function map($transaction): array
    {
        return [
            $transaction->transaction_id,
            $transaction->created_at,
        ];
    }
    
    public function headings(): array
    {
        return [
            'ID',
            'Created At',
        ];
    }

    public function query()
    {
        $transactions = \App\Models\Transactions::query()->whereIn('merchant_id', $this->merchant_ids);

        if (request()->has('filter')) {
            $filter = request()->filter;

            $transactions->when(isset($filter['status']), function ($q) use ($filter) {
                return $q->whereIn('response_code', $filter['status']);
            });

            $transactions->when(isset($filter['type']), function ($q) use ($filter) {
                return $q->whereIn('type_id', $filter['type']);
            });

            $transactions->when(isset($filter['terminal']), function ($q) use ($filter) {
                return $q->whereIn('terminal_id', $filter['terminal']);
            });

            $transactions->when(isset($filter['series']), function ($q) use ($filter) {
                return $q->where('additional_data', 'like', '%s' . $filter['series'] . '%');
            });

            $transactions->when(isset($filter['ticket']), function ($q) use ($filter) {
                return $q->where('additional_data', 'like', '%n' . $filter['ticket'] . '%');
            });

            $transactions->when(isset($filter['route']), function ($q) use ($filter) {
                return $q->where('additional_data', 'like', '%r' . $filter['route'][0] . '%');
            });

            $transactions->when(isset($filter['employee']), function ($q) use ($filter) {
                return $q->where('additional_data', 'like', '%e' . $filter['employee'][0] . '%');
            });
        
            if (isset($filter['company'])) {
                $transactions = $transactions->whereHas('companyByMerchantId', function($q) use ($filter) {
                    return $q->whereIn('merchant_id', $filter['company']);
				});
            }
            
            $transactions->when(isset($filter['date']), function ($q) use ($filter) {
                $date = explode(' - ', $filter['date']);
                $start = $date[0];
                $end = $date[1];
                if ($start == $end) {
                    $query = $q->whereDate('created_at', $start);
                } else {
                    $query = $q->whereBetween('created_at', [$start, $end]);
                }
                return $query;
            });
        }

        return $transactions;
    }
}
@ghost
Copy link

ghost commented Oct 14, 2018

Thanks for submitting the ticket. Unfortunately the information you provided is incomplete. We need to know which version you use and how to reproduce it. Please include code examples. Before we can pick it up, please check (https://github.com/Maatwebsite/Laravel-Excel/blob/3.0/.github/ISSUE_TEMPLATE.md) and add the missing information. To make processing of this ticket a lot easier, please make sure to check (https://laravel-excel.maatwebsite.nl/docs/3.0/getting-started/contributing) and double-check if you have filled in the issue template correctly. This will allow us to pick up your ticket more efficiently. Issues that follow the guidelines correctly will get priority over other issues.

@ghost ghost removed the more information needed label Oct 14, 2018
@PopovMaxim
Copy link
Author

PopovMaxim commented Oct 14, 2018

This method not work in Queue. After remove ShouldQueue from implements, map and headings works normally, but i need queues! How to fix it?

@MLouis
Copy link

MLouis commented Oct 16, 2018

Did you try to remove ShouldQueue from TransactionsExport and use

(new TransactionsExport($merchant_ids))->queue($fileName);

instead in your controller ?

@patrickbrouwers
Copy link
Member

"it does not work for me" is a bit vague. Can you please explain to us what doesn't work. Screenshots would help.

@blancomaberino
Copy link

I have a similar problem, this is my code:

namespace App\Exports;
use App\Models\Payment;
use Maatwebsite\Excel\Concerns\{WithHeadings, WithMapping, FromCollection, Exportable};
use Illuminate\Database\Eloquent\Collection;

class PaymentsExport implements FromCollection, WithHeadings, WithMapping
{
    use Exportable;

    /**
     * Export constructor
     * @param Collection $payments
     */
    public function __construct(Collection $payments) {
        $this->payments = $payments;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection() {
        return $this->payments;
    }

    /**
     * Returns headers for report
     * @return array
     */
    public function headings(): array {
        return [
            'Payment ID',
            'Owner',
            'Stripe Transaction',
            "Service",
            "Status",
            "Type",
            "Amount",
            "Payable From",
            "Last Attempt At",
            "Paid At"
        ];
    }

    /**
    * @var Payment $payment
    */
    public function map($payment): array {
        return [
            $payment->id,
            $payment->owner->full_name,
            $payment->stripe_id,
            $payment->service,
            $payment->status,
            $payment->type,
            $payment->amount / 100,
            $payment->payable_from,
            $payment->last_attempt_at,
            $payment->completed_at
        ];
    }
}

It works when exporting a CSV return Excel::download(new PaymentsExport($payments), "file.csv"); but doesn't when I try to export an excel file (be it xls or xlsx). It says the file is corrupt and won't let me open it. Any suggestion? Thank you.

@patrickbrouwers
Copy link
Member

@blancomaberino see #1673

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants