通知不会在队列中工作,但可以作为Direct工作

时间:2018-03-09 15:10:21

标签: laravel laravel-5 queue laravel-5.5

我在laravel上遇到通知问题,如果我直接发送没有队列的通知,那么这项工作也是如此

此通知需要发送电子邮件并保存在数据库中

我用它来调用notify as exemple

$user = \App\User::find(1);
    $candidato = \App\CandidatoVaga::where('id_user','=','1')->first();
    $user->notify(new \App\Notifications\ConviteVagaCandidato($candidato));

这是\ App \ Notifications \ ConviteVagaCandidato

<?php

namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ConviteVagaCandidato extends Notification implements ShouldQueue
{
use Queueable;
protected $CandidatoVaga;
/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct(\App\CandidatoVaga $CandidatoVaga)
{
    $this->CandidatoVaga = $CandidatoVaga;
}
/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['database','mail'];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
    ->greeting('Olá, '.$this->CandidatoVaga->user->DadosPessoais->nome)
    ->subject('Convite')
    ->markdown('email.convite_selecao');
}
/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
return [
        'id_vaga' => $this->CandidatoVaga->id_vaga,
        'id_user' => $this->CandidatoVaga->id_user,
        'mensagem' => 'Você foi pré selecionado para a vaga',
        'tipo' => 'Nova Vaga',
    ];
}
}

返回sql错误SQLSTATE [42601]:语法错误:7 ERRO 但没有implements ShouldQueue也可以使用

2 个答案:

答案 0 :(得分:1)

sync队列驱动程序和实际队列驱动程序之间的区别之一是排队作业如何处理存储的模型。

由于sync队列驱动程序直接在同一进程中处理作业,因此不会进行额外的工作。如果您使用Notification构建Model,则会使用该确切的模型实例。

但是,当使用真实队列时,Laravel必须序列化通知中存储的数据,以便队列工作者处理它。因为模型不能轻易地序列化,它实际上只是将模型键存储在通知上,然后当队列工作者处理该作业时,它使用存储的密钥从数据库中重新检索该模型。

根据您在评论中提到的查询,我认为您的\App\CandidatoVaga模型没有主键($primaryKey为空)。因此,查询("candidatos_vaga"."")没有主键字段,并且没有存储主键值(is null)。

我看到你已经为自己想出了一个解决方案。但是,如果您仍想尝试使用该模型,可以试试这个:

  1. 覆盖模型上的getQueueableId()方法。默认情况下,返回主键字段。但是,由于您没有定义一个,因此您需要覆盖此方法以提供一些可用于再次查找记录的唯一数据。

  2. 覆盖模型上的newQueryForRestoration()方法。默认情况下,这将使用主键字段构建查询。但是,由于您没有定义一个,您需要覆盖此方法以使用getQueueableId()方法生成的数据生成查询。

  3. 注意:这是未经测试的。我从来没有这样做过;这正是我通过源代码查看的内容。

答案 1 :(得分:0)

我能够以姑息的方式解决它

public $id_vaga;
public $id_user;
public $nome;
public $titulo_vaga;
/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct($CandidatoVaga)
{

    $this->id_vaga = $CandidatoVaga->id_vaga;
    $this->id_user = $CandidatoVaga->id_user;
    $this->nome = $CandidatoVaga->user->DadosPessoais->nome;
    $this->titulo_vaga = $CandidatoVaga->vaga->titulo_vaga;
}