love_php/app/Exports/FeedbackExport.php
2026-04-02 09:20:51 +08:00

82 lines
2.6 KiB
PHP

<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithHeadings; //设置标题
use Maatwebsite\Excel\Concerns\WithStrictNullComparison; //为空时零填充
use Maatwebsite\Excel\Concerns\ShouldAutoSize; //自动单元格尺寸
use phpDocumentor\Reflection\Types\Object_;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; //设置单元格数据格式
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\FromCollection;
use App\Models\User;
use App\Models\FeedbackHistory;
class FeedbackExport implements FromCollection ,WithHeadings,WithStrictNullComparison,WithColumnFormatting,ShouldAutoSize
{
private $status;
private $keyword;
private $limit;
private $offset;
public function __construct($status, $keyword, $limit=15, $offset)
{
$this->status = $status;
$this->keyword = $keyword;
$this->limit = $limit;
$this->offset = $offset;
}
public function headings(): array
{
return [
'反馈人ID',
'反馈人',
'反馈人头像地址',
'联系方式',
'反馈内容',
'反馈图片',
'反馈时间',
];
}
//设置列格式
public function columnFormats(): array
{
return [
'A' => NumberFormat::FORMAT_TEXT,
];
}
public function collection()
{
$status = $this->status;
$histories = FeedbackHistory::with('user')->whereHas('user', function($sql){
$sql->where('id', '>', 0);
})->where('status', $status);
$keyword = $this->keyword;
if ($keyword) {
$keyword = trim($keyword);
$histories = $histories->where(function($sql) use($keyword){
$sql->whereHas('user', function($sql) use($keyword){
$sql->where('name', 'like', '%'.$keyword.'%')
->orWhere('mobile', 'like', '%'.$keyword.'%');
})->orWhere('content', 'like', '%'.$keyword.'%');
});
}
$histories = $histories->limit($this->limit)->offset($this->offset)->orderBY('id', 'desc')->get();
$data = [];
foreach ($histories as $k => &$history) {
$data[$k]['user_id'] = $history->user_id;
$data[$k]['name'] = $history->user->name;
$data[$k]['photo'] = $history->user->photo;
$data[$k]['mobile'] = $history->user->mobile;
$data[$k]['content'] = $history->content;
$data[$k]['photos'] = $history->photos;
$data[$k]['created_at'] = $history->created_at;
}
return collect($data);
}
}