Laravel 当我将数据从控制器传递到作业时队列问题

Laravel queues issues when I pass data from controller to jobs

我在修复 Laravel 的队列部分时遇到问题。

我有一个非常有效的联系表格。我提交了表格,并向我发送了一封邮件,其中包含表格中输入的详细信息。

但问题是,如果您不使用 Laravel 队列,则由于在发送邮件之前和收到成功消息之后的等待时间,用户体验不足。

所以现在我想实现排队部分,但我没有做对。我还是菜鸟...

这是我的控制器

KontaktformController.php

<?php

namespace App\Http\Controllers;

use App\Jobs\SendEmailJob;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Session;
use App\Kontaktform;
use Illuminate\Http\Request;
use App\Mail\SendEmailMailable;

class KontaktformController extends Controller
{

    public function create()
    {
        return view('kontakt');
    }


    public function store(Request $request) {
        $this->validate($request, [
            'name' => 'required|string',
            'mobile' => 'required',
            'email' => 'required|email',
            'subject' => 'required',
            'message' => 'required',
        ]);
        /*
          Add mail functionality here.*/

        $kontakt = new Kontaktform([
            'navn'  => $request['name'],
            'mobilnr' => $request['mobile'],
            'fastnetnr' => $request['landline'],
            'mail' => $request['email'],
            'emne' => $request['subject'],
            'beskrivelse' => $request['message']
        ]);
        $kontakt->save();

        //$user['email'] = 'test@test.com';

        $job = (new SendEmailJob($request['email']))
            ->delay(Carbon::now()->addSeconds(3));

        dispatch($job);


    }
    }

SendEmailJob.php

<?php

namespace App\Jobs;

use App\Mail\SendEmailMailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;

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

    protected $user;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $email = new SendEmailMailable('test@test.dk');
        Mail::to('admin@admin.dk')->send($email);

    }
}

SendEmailMailable.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendEmailMailable extends Mailable
{
    use Queueable, SerializesModels;


    public $request;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($request)
    {
        $this->request = $request;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('test@test.dk')->view('mail.kontaktform');
    }
}

kontaktform.blad.php

{{$request['email']}}

在您的 kontaktform.blad.php 中(顺便说一句,应该命名为 kontaktform.blade.php),您希望数据是一个数组,因为您尝试在 [=] 中获取索引 ['email'] 16=] 在方法 handle() 中将电子邮件作为字符串传递。

所以你应该将其更改为以下内容:

$email = new SendEmailMailable(['email' => 'test@test.dk']);

或在控制器和作业中执行以下操作:

控制器:

$job = (new SendEmailJob($request))
        ->delay(Carbon::now()->addSeconds(3));

工作:

protected $request;

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

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    $mailable = new SendEmailMailable($this->request);
    Mail::to('admin@admin.dk')->send($mailable);

}

现在请求中的所有数据都应该在您的 blade 模板中可用。

我发现了问题。我没有将数据从 mailable 传递到我的 blade

public function build()
    {
        return $this->view( 'mail.kontaktform' )->with( [
            'request' => $this->request,
        ] );
    }