为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel 通知 - 尝试从控制器传递数据

Laravel 通知 - 尝试从控制器传递数据

PHP
海绵宝宝撒 2023-04-15 16:34:37
在HomeController.php我发送这样的通知$user->notify(new OutdatedAELocation($conSite));然后在OutdatedAELocation.php我尝试使用此数据将通知存储到数据库中。<?phpnamespace 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: []}请问,如何传递数据?
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

首先你必须定义类中的成员:


<?php


class OutdatedAELocation extends Notification implements ShouldQueue

{

    use Queueable;


    // HERE you define the members

    var $CSid;

    var $outdatedAes;

    var $link;


    // ...

}

之后dd($conSite);在构造函数的开头尝试查看是否将完整对象传递给类。


查看完整回答
反对 回复 2023-04-15
  • 1 回答
  • 0 关注
  • 58 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信