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

时间:2019-03-10 11:31:19

标签: laravel-5.6 laravel-queue

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

我有一个非常有效的联系表格。我提交了表单,并且正在将包含表单输入详细信息的邮件发送给我。

但是问题是,如果您不使用Laravel队列,则由于发送邮件之前和获得成功消息之后的等待时间,缺少UX。

所以现在我想实现排队部分,但是我做得不好。我还是一个菜鸟……

这是我的控制人

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']}}

2 个答案:

答案 0 :(得分:1)

在您的kontaktform.blad.php(应名为kontaktform.blade.php中)中,您希望数据是一个数组,因为尽管您尝试在{{1}中获取索引['email']。 }在方法SendEmailJob.php中,您将电子邮件作为字符串传递。

那么您应该将其更改为以下内容:

handle()

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

控制器:

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

工作:

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

现在,请求中的所有数据都应该在刀片服务器模板中可用。

答案 1 :(得分:0)

我发现了问题。我没有将数据从可邮件传递到刀片服务器

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