php - Laravel 通知 - 尝试从控制器传递数据
问题描述
在HomeController.php
我发送这样的通知$user->notify(new OutdatedAELocation($conSite));
然后在OutdatedAELocation.php
我尝试使用这些数据将通知存储到数据库。
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class OutdatedAELocation extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($conSite)
{
$this->CSid = $conSite->id;
$this->outdatedAes = $conSite->outdatedAes;
$this->link = $conSite->link;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
// dd($this);
return [
'conSite_id' => $this->CSid,
'outdatedAes' => $this->outdatedAes,
'link' => $this->link,
];
}
}
由于某种原因,数据不会出现在toArray
方法中。
当我dd($this)
在 __construct() 方法的末尾调用时,它就在那里:
App\Notifications\OutdatedAELocation {#1329 ▼
+id: null
+locale: null
+connection: null
+queue: null
+chainConnection: null
+chainQueue: null
+delay: null
+middleware: []
+chained: []
+"CSid": 1
+"outdatedAes": "info, "
+"link": "https://app.com/query?location=1"
}
但是,当我在方法dd($this)
的第一行调用时toArray()
,它是这样的:
App\Notifications\OutdatedAELocation {#1780 ▼
+id: "ac659b25-7ff2-4500-adc8-72e6508d50c6"
+locale: null
+connection: null
+queue: null
+chainConnection: null
+chainQueue: null
+delay: null
+middleware: []
+chained: []
}
请问,我怎样才能通过数据?
谢谢你。
解决方案
首先,您必须定义类中的成员:
<?php
class OutdatedAELocation extends Notification implements ShouldQueue
{
use Queueable;
// HERE you define the members
var $CSid;
var $outdatedAes;
var $link;
// ...
}
之后尝试dd($conSite);
在构造函数的开头查看是否将完整对象传递给类。
推荐阅读
- sql - 当 BigQuery 中没有任何“id”类型列时转置行和列
- python - S3 选择 CSV 中的检索标头
- perl - 如何使用一列索引值在单独的数据集中创建新列
- javascript - Userscript 不会将文本输入聚焦在某些页面上
- json - 将多个 JSON 数组写入文件
- autodesk-forge - Autodesk Forge - 转换和发布作业是否只完成一次还是被覆盖
- laravel - 如何保护源代码 Laravel 和 Vue
- css - 如何水平对齐 bootstrap 4 导航菜单?
- java - 为电子邮件 Intent 选择应用程序时,我的应用程序会显示为一个选项
- performance - 如何使用 ESPER 性能套件测量具有单行函数的查询的延迟和吞吐量