Laravel Datatables导出服务实现不接受查询参数

时间:2018-04-02 08:03:57

标签: php laravel datatables

我有一个带日期过滤器的服务实现。我将这些过滤器作为查询参数发送。 例如:http://localhost:8000/sales_report?start_date=2018-01-01&end_date=2018-01-02

这是控制器代码

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');
    }

这是Datatables查询代码

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;
    }

我使用的按钮如下:

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

该表工作正常。问题是当我导出时它没有考虑查询参数。它采用默认值。我发现的问题是,当我单击打印它重定向到的URL时,不包含表视图页面中存在的查询参数。有人可以建议修复吗?或者,对于我想要做的事情,laravel-datatables没有任何功能吗?

  • 操作系统:Mac Sierra
  • PHP 7.1
  • Laravel 5.4。*
  • Laravel-Datatables 1.0

1 个答案:

答案 0 :(得分:1)

Minified Ajax应包含查询参数:

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