Laravel。如何获取数据库通知的ID?
Laravel. How to get id of database notification?
我使用数据库通知,在通知代码中我有方法 toDatabase
:
public function toDatabase($notifiable)
{
$user = \App\SomeUsers::where('id', $notifiable->id)->first();
return [
'message' => $message,
];
}
它returns正在发送到via
当前通知方法中提到的数据库通道的数据数组:
public function via($notifiable)
{
return ['database'];
}
一切如常,但是......问题是我需要当前通知文件中数据库中的通知ID,以便我可以将消息(从当前通知文件)广播到前端,其中包含数据库中通知的ID (所以我可以以某种方式识别它以标记为已读)。如何获得?
P.S。而且,数据库通知可能是可排队的,所以……好像拿不到id……
P.P.S 换句话说我需要包含 ["id" => "id of just added corresponding database notification"]
.
的广播消息
<?php
namespace App\Notifications;
use App\Channels\SocketChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Redis;
class MyCustomNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($param)
{
$this->param = $param;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
$channels = ['database'];
return $channels;
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
info("This is the current notification ID, it's generated right here before inserting to database");
info($this->id);
return [
'id' => **$this->id**,
'message' => 'Notification message',
];
}
}
$this->id 解决问题。
https://laracasts.com/discuss/channels/laravel/get-database-notification-id-in-push-notification
P.S。 我想提请注意一个事实。当我发布这个问题时,我知道 $this->id,但我无法让它工作。原因是:当我从顶层深入到我的目标代码时,我对代码进行了更改,但它们并不适用。原因是排队。您需要重新启动 laravel worker 以将设置应用为 Laravel 缓存逻辑,或者您需要暂时删除那些:实施 ShouldQueue 并使用 Queueable。
为了检索 Laravel 中通知 table 的实际 ID,您需要将 ID 列转换为字符串。首先,您需要创建一个名为 Notification
.
的新模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
/**
* Cast variables to specified data types
*
* @var array
*/
protected $casts = [
'data' => 'array',
'id' => 'string'
];
}
这样,如果您检索模型,它将为您提供 table 的实际 ID。
{
"id": "212829d6-5579-449f-a8e5-e86f0a08e0f9",
"type": "App\Notifications\CronFailureNotification",
....
}
接受的答案在Laravel 8中没有为我提供解决方案。要正确获取通知的 ID,请监听 NotificationSent 事件并在那里检索 ID。示例代码:
EventServiceProvider.php
protected $listen = [
NotificationSent::class => [
DispatchBroadcastNotification::class
]
];
DispatchBroadcastNotification.php
<?php
namespace App\Listeners;
use App\Notifications\BroadcastNotification;
use Illuminate\Notifications\Events\NotificationSent;
class DispatchBroadcastNotification
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param NotificationSent $event
* @return void
*/
public function handle(NotificationSent $event)
{
$event->notifiable->notify(
new BroadcastNotification($event->notification->id)
);
}
}
BroadcastNotification.php
<?php
namespace App\Notifications;
use App\Models\Tenant\User;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;
class BroadcastNotification extends Notification implements ShouldBroadcast
{
public $notificationId;
public function __construct($notificationId)
{
$this->notificationId = $notificationId;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via(User $notifiable): array
{
return ['broadcast'];
}
public function toBroadcast(User $notifiable)
{
return new BroadcastMessage([
'notificationId' => $this->notificationId
]);
}
}
我使用数据库通知,在通知代码中我有方法 toDatabase
:
public function toDatabase($notifiable)
{
$user = \App\SomeUsers::where('id', $notifiable->id)->first();
return [
'message' => $message,
];
}
它returns正在发送到via
当前通知方法中提到的数据库通道的数据数组:
public function via($notifiable)
{
return ['database'];
}
一切如常,但是......问题是我需要当前通知文件中数据库中的通知ID,以便我可以将消息(从当前通知文件)广播到前端,其中包含数据库中通知的ID (所以我可以以某种方式识别它以标记为已读)。如何获得?
P.S。而且,数据库通知可能是可排队的,所以……好像拿不到id……
P.P.S 换句话说我需要包含 ["id" => "id of just added corresponding database notification"]
.
<?php
namespace App\Notifications;
use App\Channels\SocketChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Redis;
class MyCustomNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($param)
{
$this->param = $param;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
$channels = ['database'];
return $channels;
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
info("This is the current notification ID, it's generated right here before inserting to database");
info($this->id);
return [
'id' => **$this->id**,
'message' => 'Notification message',
];
}
}
$this->id 解决问题。
https://laracasts.com/discuss/channels/laravel/get-database-notification-id-in-push-notification
P.S。 我想提请注意一个事实。当我发布这个问题时,我知道 $this->id,但我无法让它工作。原因是:当我从顶层深入到我的目标代码时,我对代码进行了更改,但它们并不适用。原因是排队。您需要重新启动 laravel worker 以将设置应用为 Laravel 缓存逻辑,或者您需要暂时删除那些:实施 ShouldQueue 并使用 Queueable。
为了检索 Laravel 中通知 table 的实际 ID,您需要将 ID 列转换为字符串。首先,您需要创建一个名为 Notification
.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
/**
* Cast variables to specified data types
*
* @var array
*/
protected $casts = [
'data' => 'array',
'id' => 'string'
];
}
这样,如果您检索模型,它将为您提供 table 的实际 ID。
{
"id": "212829d6-5579-449f-a8e5-e86f0a08e0f9",
"type": "App\Notifications\CronFailureNotification",
....
}
接受的答案在Laravel 8中没有为我提供解决方案。要正确获取通知的 ID,请监听 NotificationSent 事件并在那里检索 ID。示例代码:
EventServiceProvider.php
protected $listen = [
NotificationSent::class => [
DispatchBroadcastNotification::class
]
];
DispatchBroadcastNotification.php
<?php
namespace App\Listeners;
use App\Notifications\BroadcastNotification;
use Illuminate\Notifications\Events\NotificationSent;
class DispatchBroadcastNotification
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param NotificationSent $event
* @return void
*/
public function handle(NotificationSent $event)
{
$event->notifiable->notify(
new BroadcastNotification($event->notification->id)
);
}
}
BroadcastNotification.php
<?php
namespace App\Notifications;
use App\Models\Tenant\User;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;
class BroadcastNotification extends Notification implements ShouldBroadcast
{
public $notificationId;
public function __construct($notificationId)
{
$this->notificationId = $notificationId;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via(User $notifiable): array
{
return ['broadcast'];
}
public function toBroadcast(User $notifiable)
{
return new BroadcastMessage([
'notificationId' => $this->notificationId
]);
}
}