php - Laravel excel多表使用视图返回多表但为空
问题描述
我正在尝试导出具有多个工作簿的工作表并从视图中生成每个工作簿。我创建了 5 个不同的 .blade 文件,其中包含 HTML 表,并希望从每个 .blade 文件生成每个工作簿。
我正在尝试导出具有多个工作簿的工作表并从视图中生成每个工作簿。我创建了 5 个不同的 .blade 文件,其中包含 HTML 表,并希望从每个 .blade 文件生成每个工作簿。
<?php
namespace App\Exports;
use App\Models\GenerateQrCode;
use App\Models\Scan;
use DateInterval;
use DatePeriod;
use DateTime;
use Hashids\Hashids;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Excel;
class ScansExport implements FromView,WithMultipleSheets {
public $id;
public $from;
public $to;
public function __construct($id,$request) {
$this->from = $request['from'];
$this->to = $request['to'];
$this->id = $id;
}
public function getScansCountGroupBy($id, $field) {
return Scan::where('qr_code_id', $id)->select($field, \DB::raw('count(*) as scans'))->groupBy($field)->orderBy('created_at', 'asc')->get();
}
public function view(): View {
$id = \Hashids::decode($this->id)[0];
$data['qrCode'] = GenerateQrCode::find($id);
if ($this->from && $this->to) {
$from = $this->from . ' 00:00:00';
$to = $this->to . ' 23:59:59';
$data['from'] = $this->from;
$data['to'] = $this->to;
} else {
$from = date('Y-m-d', strtotime("-30 days")) . ' 00:00:00';
$to = date('Y-m-d') . ' 23:59:59';
$data['from'] = date('Y-m-d', strtotime("-30 days"));
$data['to'] = date('Y-m-d');
}
$data['scansList'] = Scan::where('qr_code_id', $id)->whereBetween('created_at', [$from, $to])->orderBy('created_at', 'desc')->get();
$data['firstScan'] = Scan::where('qr_code_id', $id)->whereBetween('created_at', [$from, $to])->first();
$data['scansDataPoints'] = [];
$data['uniqueUsersDataPoints'] = [];
if (!empty($data['firstScan'])) {
$data['countryCount'] = Scan::where('qr_code_id', $id)->whereBetween('created_at', [$from, $to])->where('country', $data['firstScan']->country)->count();
$data['deviceCount'] = Scan::where('qr_code_id', $id)->whereBetween('created_at', [$from, $to])->where('device', $data['firstScan']->device)->count();
/*
** Countries
** Cities
** Languages
*/
$data['countries'] = $this->getScansCountGroupBy($id, 'country');
$data['cities'] = $this->getScansCountGroupBy($id, 'city');
$data['languages'] = $this->getScansCountGroupBy($id, 'language');
/*
** Devices
** Platforms
** Browsers
*/
$data['devices'] = $this->getScansCountGroupBy($id, 'device');
$data['platforms'] = $this->getScansCountGroupBy($id, 'platform');
$data['browsers'] = $this->getScansCountGroupBy($id, 'browser');
// $period = new \DatePeriod(
// new \DateTime(date('Y-m-d', strtotime("-30 days"))),
// new \DateInterval('P1D'),
// new \DateTime(date('Y-m-d', strtotime("+1 days")))
// );
$period = new \DatePeriod(
new \DateTime($from),
new \DateInterval('P1D'),
new \DateTime($to)
);
/*
** Scans Graph
*/
$scans = Scan::where('qr_code_id', $id)->whereBetween('created_at', [$from, $to])->select(\DB::raw('DATE(created_at) as date'), \DB::raw('count(*) as scans'))->groupBy('date')->orderBy('created_at', 'asc')->get()->toArray();
$scansDataPoints = array();
foreach ($period as $key => $value) {
$date = $value->format('Y-m-d');
$index = array_search($value->format('Y-m-d'), array_column($scans, 'date'));
if ($index !== false) {
$scansDataPoints[$date] = $scans[$index]['scans'];
} else {
$scansDataPoints[$date] = 0;
}
}
$data['scansDataPoints'] = $scansDataPoints;
/*
** Unique Users Graph
*/
$unique_users = Scan::where('qr_code_id', $id)->whereBetween('created_at', [$from, $to])->select(\DB::raw('DATE(created_at) as date'), \DB::raw('count(DISTINCT ip) as unique_count'))->groupBy('date')->orderBy('created_at', 'asc')->get()->toArray();
$uniqueUsersDataPoints = array();
foreach ($period as $key => $value) {
$date = $value->format('Y-m-d');
$index = array_search($value->format('Y-m-d'), array_column($unique_users, 'date'));
if ($index !== false) {
$uniqueUsersDataPoints[$date] = $unique_users[$index]['unique_count'];
} else {
$uniqueUsersDataPoints[$date] = 0;
}
}
$data['uniqueUsersDataPoints'] = $uniqueUsersDataPoints;
}
// $array = [
// view('frontend.dashboard.export-statistics.qrcode-countries', $data),
// view('frontend.dashboard.export-statistics.qrcode-cities', $data),
// ];
return $data;
}
public function sheets(): array {
$data = $this->view();
return [
view('frontend.dashboard.export-statistics.qrcode-countries', $data),
view('frontend.dashboard.export-statistics.qrcode-cities', $data),
];
}
}
解决方案
您不能将视图作为图纸传递。您必须为每个视图创建单独的图纸导出。然后,您可以使用WithMultipleSheets
关注点将它们组合起来。
CountryExport.php
class CountriesExport implements FromView
{
public function view(): View
{
$data = ...
return view('frontend.dashboard.export-statistics.qrcode-countries', $data);
}
}
城市出口.php
class CitiesExport implements FromView
{
public function view(): View
{
$data = ...
return view('frontend.dashboard.export-statistics.qrcode-cities', $data);
}
}
扫描导出.php
class ScansExport implements WithMultipleSheets
{
public function sheets(): array
{
return [
new CountriesExport(),
new CitiesExport(),
];
}
}
推荐阅读
- angular - Angular 如何在没有 NgModel 的情况下默认检查单选按钮
- firebase - 在flutter firebase中实现全文搜索时出错
- azure-devops - Azure Pipelines 中的此上下文中不允许映射值
- python - 了解 matplotlib set_bad 颜色图
- reactjs - 对象不支持 IE 11 上的属性或方法“包含”
- php - CORS 预检请求被拒绝
- python - 如何将 xml 动态转换为 csv
- javascript - 我有一组索引和品种,我想列出可能的排列
- wix - Wix 工具集 - 如何使用二进制文件
- detect - iOS14 - CMMotionActivityManager startActivityUpdatesToQueue 不工作