【问题标题】:Notification don't work on queue but work as Direct通知不适用于队列,但可作为直接通知
【发布时间】:2018-03-09 15:10:20
【问题描述】:

我在 laravel 上的通知有问题,如果我直接发送通知而不使用队列也可以

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

我用这个来调用notify作为例子

$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]: Syntax error: 7 ERRO 但没有implements ShouldQueue 也可以

【问题讨论】:

  • 什么是sql错误?
  • SQLSTATE[42601]:语法错误:7 错误:定界标识符在“””中或附近的大小为零第 1 行:... * 来自“candidatos_vaga”,其中“candidatos_vaga”。“”是null... ^ (SQL: select * from "candidatos_vaga" where "candidatos_vaga"."" is null limit 1)
  • 尝试登录$this-&gt;CandidatoVaga并在laravel.log中查看
  • 我认为这是问题所在,我删除了所有 $this->CandidatoVaga 并开始工作,但我做错了什么?
  • 可能模型没有正确加载模型,之前遇到过序列化问题。

标签: laravel laravel-5 queue laravel-5.5


【解决方案1】:

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

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

但是,当使用真正的队列时,Laravel 必须序列化存储在通知中的数据,以供队列工作者处理。由于模型不容易序列化,它实际上只是将模型键存储在通知中,然后当队列工作人员处理该作业时,它会使用存储的键从数据库中重新检索该模型。

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

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

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

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

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

【讨论】:

  • 在这个模式中我可以使用堆肥主键,如果我在模型上设置不能解决这个问题?
  • @GuilhermeFreire 不,Laravel 的 eloquent 不支持复合主键。
【解决方案2】:

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

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;
}

【讨论】:

    猜你喜欢
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多