【问题标题】:Laravel. How to get id of database notification?拉拉维尔。如何获取数据库通知的ID?
【发布时间】:2020-09-02 18:46:23
【问题描述】:

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

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

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

public function via($notifiable)

    {
        return ['database'];
    }

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

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

【问题讨论】:

  • 能否分享一下您当前显示通知的代码?
  • 克里斯托夫,感谢您的反馈。我找到了答案,看起来这正是我所需要的。我会发布它。

标签: laravel laravel-notification


【解决方案1】:

为了在 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'
    ];
}

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

 {
        "id": "212829d6-5579-449f-a8e5-e86f0a08e0f9",
        "type": "App\\Notifications\\CronFailureNotification",
        ....
    }

【讨论】:

    【解决方案2】:
    <?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

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

    【讨论】:

    • 对我来说使用use Illuminate\Notifications\Notification; 而不是use Notification; 解决了问题
    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 2019-02-21
    • 2018-08-26
    • 2017-08-22
    • 2020-11-11
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    相关资源
    最近更新 更多