Laravel 通知 - 试图从控制器传递数据
Laravel Notifications - trying to pass data from controller
在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
方法。
当我在 __construct() 方法的末尾调用 dd($this)
时,它都在那里:
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"
}
然而,当我在 toArray()
方法的第一行调用 dd($this)
时,是这样的:
App\Notifications\OutdatedAELocation {#1780 ▼
+id: "ac659b25-7ff2-4500-adc8-72e6508d50c6"
+locale: null
+connection: null
+queue: null
+chainConnection: null
+chainQueue: null
+delay: null
+middleware: []
+chained: []
}
请问如何传递数据?
谢谢。
首先你必须在 class:
中定义成员
<?php
class OutdatedAELocation extends Notification implements ShouldQueue
{
use Queueable;
// HERE you define the members
var $CSid;
var $outdatedAes;
var $link;
// ...
}
之后在构造函数的开头尝试 dd($conSite);
以查看是否将完整对象传递给 class。
在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
方法。
当我在 __construct() 方法的末尾调用 dd($this)
时,它都在那里:
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"
}
然而,当我在 toArray()
方法的第一行调用 dd($this)
时,是这样的:
App\Notifications\OutdatedAELocation {#1780 ▼
+id: "ac659b25-7ff2-4500-adc8-72e6508d50c6"
+locale: null
+connection: null
+queue: null
+chainConnection: null
+chainQueue: null
+delay: null
+middleware: []
+chained: []
}
请问如何传递数据?
谢谢。
首先你必须在 class:
中定义成员<?php
class OutdatedAELocation extends Notification implements ShouldQueue
{
use Queueable;
// HERE you define the members
var $CSid;
var $outdatedAes;
var $link;
// ...
}
之后在构造函数的开头尝试 dd($conSite);
以查看是否将完整对象传递给 class。