php 导出CSV并将数据传递到邮件视图时出现TypeError- Laravel 9

vmdwslir  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(113)

我在Laravel 9中的队列作业有两个问题。
在一个作业中,我试图将一些关系数据导出到.CSV文件,并希望在导出完成时通知用户。
这是我的控制器:

$report = new ExportReport();
  $this->dispatch($report);

这是我的Jobs/ExportReport.php文件:

<?php

namespace App\Jobs;
use Illuminate\Support\Facades\Mail;
use App\Models\Report;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;

class ExportReport implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Execute the job.
     * @return $response
     */
    public function handle()
    {
        // Generate our file name
        $filename="Report_Export_" . date("YmdHis") . time() . ".csv";

        // Set the file path to the temporary location
        $handler = Storage::path('public/').$filename;

        // Open file handler for writing output
        $file = fopen($handler, 'w');

        /*
         * Add headers
         */
        $headers = [
            'Inspection Number',
            'Identification of Inspection',
            'Request or Planned Date',
            'Designation of Inspection',
            'Type of Inspection',
            'Inspector Name',
            'Inspector Last Name',
            'Inspection End Date',
            'Result of Inspection',
            'Status of Inspection',
            'Reject Reason',
            'Reason of Cancellation',
            'Comments'
        ];
        fputcsv($file, $headers);

        Report::chunk(1000, function ($reports) use ($headers, $file) {
            foreach ($reports as $report) {
                fputcsv($file, [
                $report->inspection_number,
                $report->identification_of_inspection,
                $report->request_or_planned_date,
                $report->designation_of_inspection,
                $report->type_of_inspection ?? '',
                $report->employee->first_name ?? '',
                $report->employee->last_name ?? '',
                $report->inspection_end_date,
                $report->result_of_inspection->result ?? '',
                $report->status_of_inspection->status ?? '',
                $report->reject_reason->agg ?? '',
                $report->reason_of_cancellation->reason_of_cancellation ?? '',
                $report->comments ?? ''
                ]);
            }
        });

        fclose($file);

        Storage::put('public/', $file );
        Mail::to('test@mail.com')->send(new \App\Mail\ExportReport());
    }

}

\App\Mail\ExportReport.php

public function build()
    {
        return $this->markdown('mails.export-report');
    }

"这些是我的问题"

**1-**我无法将任何数据传递到我的邮件视图。我需要传递$filename以链接邮件中的导出文件,并需要传递auth()->user()->email以了解要发送给哪个用户。
**2-**作业导出文件,但每次都失败并显示异常消息:TypeError: League\Flysystem\Filesystem::write(): Argument #2 ($contents) must be of type string, resource given, called in...通常它生成文件,但如果我有一个严重的问题,在失败的工作,这将是不可能的跟踪。我必须解决这个问题。

我被困住了,什么都不知道。

nnsrf1az

nnsrf1az1#

我用这种方法解决了这个问题。它可能会帮助其他用户。我删除了一行:

Storage::put('public/', $file );

Jobs/ExportReport.pdf

protected $user;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct()
{
    $this->user = \auth()->user();
}

/**
 * Execute the job.
 * @return $response
 */
public function handle()
{

    // Generate our file name
    $filename="Report_Export_" . date("YmdHis") . time() . ".csv";

    $data = [
        'user' => $this->user,
        'filename'=>$filename,
    ];

    //dd($data);
    //dd(\auth()->user()->email);

    Mail::send('mails.export-report', $data, function ($m) use($data){
        $m->from('no-reply@mail.com', 'Sender Name');
        $m->to($this->user['email'], $this->user['first_name'].' '.$this->user['last_name'])->subject('Your export file is ready!');
    });

相关问题