php - Laravel 5.7 : Argument 1 passed to

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

我实现了 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>

Controller
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();
    // }
}

最佳答案

问题是这一行:

public function to_date(date $to)

什么是date这里?没有date PHP 中的默认类。在这种情况下,您可能想删除 date从这里完全或替换为 string :
public function to_date(string $to)

关于php - Laravel 5.7 : Argument 1 passed to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52931847/

相关文章:

php - 使用命名空间和现有代码来使用项目

javascript - 使用 javascript 而不是 php 构建对象

php - Laravel 5.4 Raw 未按预期工作,但在 mySQL 中工作

matlab - MATLAB 与 namespace 最接近的东西是什么?

css - 使用 Laravel 的 elixir 导入 CSS 文件时出现 ErrorException

mysql - Laravel 当足够n条记录时如何批量插入数据库?

c# - 根和第一个 child 上的 XDocument 命名空间

PHP MYSQL INTERVAL 显示还剩多少时间?

php - 使用 PHP CLI 通过 imap 和 mysql 或其他语言处理电子邮件检索

php - Yii,如何让 Controller 代码在所有 View 中运行