通知不适用于队列,但作为直接工作
Notification don't work on queue but work as Direct
我对 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',
];
}
}
这个return一个sql错误SQLSTATE[42601]:语法错误:7 ERRO
但没有 implements ShouldQueue
也可以工作
治标不治本
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;
}
sync
队列驱动程序和真正的队列驱动程序之间的区别之一是排队作业如何处理存储的模型。
由于 sync
队列驱动程序直接在同一进程中处理作业,因此没有完成额外的工作。如果您使用 Model
构建 Notification
,它会使用该模型实例。
但是,当使用真正的队列时,Laravel 必须序列化存储在通知中的数据,以便队列工作者处理它。因为模型不能轻易序列化,它实际上只是将模型键存储在通知上,然后当队列工作者处理该作业时,它 re-retrieves 使用存储的键从数据库中建模。
根据您在评论中提到的查询,在我看来您的 \App\CandidatoVaga
模型没有主键($primaryKey
为空)。正因如此,没有主键字段查询("candidatos_vaga".""
),也没有存储主键值(is null
)。
我看到你已经为自己想出了一个解决方案。但是,如果您仍然想尝试只使用该模型,您可以试试这个:
覆盖模型上的 getQueueableId()
方法。默认情况下,此 returns 主键字段。但是,由于您没有定义,您需要覆盖此方法以提供一些可用于再次查找您的记录的唯一数据。
覆盖模型上的 newQueryForRestoration()
方法。默认情况下,这会使用主键字段构建查询。但是,由于您没有定义,您需要覆盖此方法以使用 getQueueableId()
方法生成的数据生成查询。
注意:这是未经测试的。我从来没有这样做过;这正是我通过源代码看到的。
我对 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',
];
}
}
这个return一个sql错误SQLSTATE[42601]:语法错误:7 ERRO
但没有 implements ShouldQueue
也可以工作
治标不治本
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;
}
sync
队列驱动程序和真正的队列驱动程序之间的区别之一是排队作业如何处理存储的模型。
由于 sync
队列驱动程序直接在同一进程中处理作业,因此没有完成额外的工作。如果您使用 Model
构建 Notification
,它会使用该模型实例。
但是,当使用真正的队列时,Laravel 必须序列化存储在通知中的数据,以便队列工作者处理它。因为模型不能轻易序列化,它实际上只是将模型键存储在通知上,然后当队列工作者处理该作业时,它 re-retrieves 使用存储的键从数据库中建模。
根据您在评论中提到的查询,在我看来您的 \App\CandidatoVaga
模型没有主键($primaryKey
为空)。正因如此,没有主键字段查询("candidatos_vaga".""
),也没有存储主键值(is null
)。
我看到你已经为自己想出了一个解决方案。但是,如果您仍然想尝试只使用该模型,您可以试试这个:
覆盖模型上的
getQueueableId()
方法。默认情况下,此 returns 主键字段。但是,由于您没有定义,您需要覆盖此方法以提供一些可用于再次查找您的记录的唯一数据。覆盖模型上的
newQueryForRestoration()
方法。默认情况下,这会使用主键字段构建查询。但是,由于您没有定义,您需要覆盖此方法以使用getQueueableId()
方法生成的数据生成查询。
注意:这是未经测试的。我从来没有这样做过;这正是我通过源代码看到的。