我实现了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');
}
PostExport
<?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();
// }
}
答案 0 :(得分:0)
问题是这一行:
public function to_date(date $to)
这里的date
是什么?默认情况下,PHP中没有date
类。在这种情况下,您可能希望将date
从此处完全删除或替换为string
:
public function to_date(string $to)