【问题标题】:Export data with custom parameters Laravel Excel使用自定义参数导出数据 Laravel Excel
【发布时间】:2020-04-26 17:49:26
【问题描述】:

我目前使用的是https://laravel-excel.com/的3.1版我想查询数据并将它们下载到excel文件中,但它总是返回一个空白表。

我的路线:

Route::get('/reports/excel', 'ReportController@excel');

我的 ReportsExport 控制器:

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;


class ReportsExport implements FromView, WithEvents, ShouldAutoSize
{
    use Exportable;

    private $from, $to;

    public function __construct(String $from, String $to) {

        $this->from = $from;
        $this->to = $to;
    }

    public function view(): View
    {
        return view('reports.sample', [
            'reports' => Report::whereBetween('created_at', [$this->from, $this->to])->get()
        ]);
    }

下载控制器

    public function excel(Request $request) {
    $from = Carbon::parse($request->get('from'));
    $to = Carbon::parse($request->get('to'));

    return Excel::download(new ReportsExport($from, $to), 'reports.xlsx');
}

你能告诉我我在这里缺少什么吗?任何帮助,将不胜感激。谢谢!

【问题讨论】:

标签: php laravel


【解决方案1】:

我已经成功了。这是我所做的:

Exports.php

    class ReportsExport implements FromView, WithEvents, ShouldAutoSize
{
    use Exportable;

    protected $from, $to;

    public function __construct(String $from, String $to) {

        $this->from = $from;
        $this->to = $to;
    }

    public function view(): View
    {
        return view('reports.sample', [
            'reports' => User::find(Auth::user()->id)->reports()
                   ->whereDate('created_at', '>=', $this->from)
                   ->whereDate('created_at', '<=', $this->to)
                   ->get(),


        ]);
    }

我的控制器

    public function excel(Request $request) {
    $from = Carbon::parse($request->get('from'));
    $to = Carbon::parse($request->get('to'));

    return Excel::download(new ReportsExport($from, $to), 'reports.xlsx');
}

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多