拉拉维尔。如何获取数据库通知的ID?

Joh*_*ith 6 laravel laravel-notification

我使用数据库通知,在通知代码中我有方法toDatabase

public function toDatabase($notifiable)
    {
        $user = \App\SomeUsers::where('id', $notifiable->id)->first();
        return [
             'message' => $message,
        ];
    }
Run Code Online (Sandbox Code Playgroud)

它返回正在发送到via当前通知方法中提到的数据库通道的数据数组:

public function via($notifiable)

    {
        return ['database'];
    }
Run Code Online (Sandbox Code Playgroud)

一切都像往常一样,但是......问题是我需要在当前通知文件中的数据库中的通知ID,以便我可以将消息(从当前通知文件)广播到包含数据库中通知ID的前端(所以我可以以某种方式将其标识为已读)。如何获得?

PS 此外,数据库通知可能是可排队的,所以...似乎我无法获得 id... PPS 另一个词我需要广播消息,其中包含["id" => "id of just added corresponding database notification"].

小智 7

为了检索 Laravel 中通知表的实际 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'
    ];
}
Run Code Online (Sandbox Code Playgroud)

这样,如果您检索模型,它将为您提供表的实际 ID。

 {
        "id": "212829d6-5579-449f-a8e5-e86f0a08e0f9",
        "type": "App\\Notifications\\CronFailureNotification",
        ....
    }
Run Code Online (Sandbox Code Playgroud)


Joh*_*ith 6

<?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',

        ];
    }


} 
Run Code Online (Sandbox Code Playgroud)

$this->id解决了这个问题。

https://laracasts.com/discuss/channels/laravel/get-database-notification-id-in-push-notification

PS我想提请注意一个事实。当我发布这个问题时,我知道$this->id,但我无法让它工作。原因是:当我从顶层深入研究目标代码时,我对代码进行了更改,但它们没有应用。原因是排队。您需要重新启动 Laravel Worker 以应用 Laravel 缓存逻辑的设置,或者您需要暂时删除那些:实现 ShouldQueue 并使用 Queueable。