Laravel Excel 如何只导出今天的记录

How to export only today's records in Laravel Excel

我只想从我的 table 中导出今天的记录,而不是整个数据,我使用 Carbon 也没有用,它只是简单地导出空的 excel 文件。我在这里分享我的代码快照,请帮助我。 我正在使用 laravel 7 和 laravel-Excel 软件包的最新版本。

   <?php

namespace App\Exports;

use App\CosQueue;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Carbon\Carbon;
class CosQueueExport implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $todaydate = date('Y-m-d');
        return CosQueue::get(array('full_name', 'job_title','meeting_with','subject','date'))->where('created_at',$todaydate);
        

    }

    

    public function headings():array{
        return[
            "اسم",
            'وظیفه',
            'ملاقات با',
            'موضوع',
            'تاریخ'

        ];
    }

    public function registerEvents(): array
    {

       
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $cellRange = 'A1:W1'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setName('calibri');
              
            },
        ];

       
    }
    
}

你可以做其中之一

public function collection()
{
   // Carbon::today() === today()
   return CosQueue::whereDate('created_at', Carbon::today())->get(array('full_name', 'job_title','meeting_with','subject','date'));
}

此外,您还可以

public function collection()
{
   return CosQueue::whereDate('created_at', date('Y-m-d'))->get(array('full_name', 'job_title','meeting_with','subject','date')); 
}