【问题标题】:Laravel 5.7: Argument 1 passed toLaravel 5.7:参数 1 传递给
【发布时间】:2018-10-22 14:33:45
【问题描述】:

我实现了 maatwebiste(导出到 excel)并尝试改进它,管理员可以选择“从日期:”到“截止日期:”进行导出。

提交表单时遇到错误"Argument 1 passed to App\Exports\PostExport::to_date() must be an instance of App\Exports\date, string given, called in "

注意:我使用 var_dump 来检查它是否有价值

问题:如何将我的值传递到我的 PostExport 文件?(我的导出到 excel 文件)

查看

<form action="{{ route('export.excel') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="form-group">
        <div class="row">
            <div class="col-xs-3">
                <p>To: <input type="text" id="to_datepicker" name="to_datepicker"></p>
                <p>From: <input type="text" id="from_datepicker" name="from_datepicker"></p>
                <button type="submit" class="btn btn-primary ">Export</button>
            </div>
        </div>
    </div>
</form>

控制器

public function excel(Request $request)
    {
        $to_datepicker = $request->to_datepicker;
        $from_datepicker = $request->from_datepicker;
        echo "<pre>";
            var_dump($to_datepicker);
            var_dump($from_datepicker);
        echo "</pre>";
        // die;

        // Excel::create('Filename', function($excel) { 

        //  // Call writer methods here
        //  Excel::selectSheets('title')->load();


        // });

        return (new PostExport)->to_date($to_datepicker)->from_date($from_datepicker)->download('invoices.xlsx');

    }

后导出

<?php

namespace App\Exports;

use App\Post;
use Illuminate\Contracts\Support\Responsable;
use Maatwebsite\Excel\Concerns\FromQuery;
// use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;

class PostExport implements FromQuery, Responsable
{
    use Exportable;
    /**
    * @return \Illuminate\Support\Collection
    */
    // private $fileName = 'invoices.xlsx';

    public function to_date(date $to)
    {
        $this->to = $to;

        return $this;

    }
    public function from_date(date $from)
    {
        $this->from = $from;

        return $this;
    }


    public function query()
    {
        // return Post::query()->whereYear('created_at', $this->year)
        //                    ->orWhere('created_at',$this->month);

        return Post::query()->whereBetween('created_at',[$this->to,$this->from]);
    }
    // public function collection()
    // {

    //     return Post::all();
    // }
}

【问题讨论】:

    标签: php laravel-5 namespaces maatwebsite-excel laravel-5.7


    【解决方案1】:

    问题出在这一行:

    public function to_date(date $to)
    

    这里的date 是什么? PHP 中默认没有 date 类。在这种情况下,您可能想从此处完全删除 date 或替换为 string

    public function to_date(string $to)
    

    【讨论】:

    • 实际上我已经这样做了,是的,它可以工作,但文件已损坏。 “Excel 无法打开文件'...',因为文件格式或文件扩展名无效。验证文件没有损坏并且文件扩展名与文件扩展名匹配”
    • 是的,但这完全是另一个问题。在问题中,您询问了参数错误。
    猜你喜欢
    • 1970-01-01
    • 2019-02-05
    • 2020-03-24
    • 2023-03-10
    • 2019-08-01
    • 2017-12-23
    • 1970-01-01
    • 2021-01-19
    • 2015-10-09
    相关资源
    最近更新 更多