yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.75k stars 858 forks source link

Query Param is not applying when exporting to excel #1686

Closed nahid1991 closed 6 years ago

nahid1991 commented 6 years ago

I have a service implementation with date filter. I am sending those filters as query parameter. For example: http://localhost:8000/sales_report?start_date=2018-01-01&end_date=2018-01-02

This is the Controller code

public function salesReport(SalesDatatable $dataTable, Request $request)
    {
        if (!Auth::user()->can('access-sales-report')) {
            abort(401);
        }
        return $dataTable->with([
            'start_date' => $request->get('start_date'),
            'end_date' => $request->get('end_date'),
        ])->render('reports.sales_reports.index');
    }

This is the Datatables query code

public function query()
    {
        $query = Invoice::select(DB::raw('GROUP_CONCAT("i_p"."name") AS name'), DB::raw('GROUP_CONCAT("i_p"."quantity") AS quantity'),
                            'invoices.id', 'invoices.invoice_no','i_p.invoice_id', 'invoices.created_at', 'invoices.product_price_exclusive_vat as total_without_vat',
                            'invoices.card_received', 'invoices.cash_received', 'invoices.total_received', 'invoices.vat_amount', 'invoices.product_discount_amount as discount',
                            DB::raw('GROUP_CONCAT("product_combinations"."barcode") AS barcode'), DB::raw('GROUP_CONCAT("i_p"."price") AS price'))
                            ->join(DB::raw('(SELECT "invoice_products"."invoice_id", "invoice_products"."product_id", "products"."name", "invoice_products"."quantity", 
                            "product_prices"."price" FROM "invoice_products" JOIN "products" ON "invoice_products"."product_id" = "products"."id" 
                            JOIN "product_prices" ON "products"."id" = "product_prices"."product_id") AS i_p'), 'invoices.id', '=', 'i_p.invoice_id')
                            ->join('product_combinations', 'product_combinations.product_id', '=', 'i_p.product_id');

        if(!empty($this->request()->get('start_date'))) {
            $query->where('invoices.created_at', '>=', $this->start_date);
        } else {
            $query->where('invoices.created_at', '>=', Carbon::today()->subDays(7)->toDateString());
        }

        if(!empty($this->request()->get('end_date'))) {
            $query->where('invoices.created_at', '<=', $this->end_date);
        } else {
            $query->where('invoices.created_at', '<=', Carbon::today()->toDateString());
        }

        $query->groupBy('i_p.invoice_id');
        return $query;
    }

I am using the buttons like:

public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->parameters([
                        'buttons'      => ['excel','print'],
                        'dom'          => 'lftiprB',
                        'pageLength'   => 25
                    ]);
    }

The table works fine. Problem is when I export it doesn't consider the query params. It takes the default values. Any solution?

nahid1991 commented 6 years ago

->minifiedAjax('', null, request()->only(['start_date', 'end_date']))

solved it.