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

in HomeController.php I send notification like this $user->notify(new OutdatedAELocation($conSite));

then in OutdatedAELocation.php I am trying to use this data to store a notification to DB.

<?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,
        ];
    }
}

for some reason, the data wont come to the toArray method.

when I call dd($this) at the end of __construct() method, its all there:

App\Notifications\OutdatedAELocation {#1329 ▼
  +id: null
  +locale: null
  +connection: null
  +queue: null
  +chainConnection: null
  +chainQueue: null
  +delay: null
  +middleware: []
  +chained: []
  +"CSid": 1
  +"outdatedAes": "344 LZB 60cm, 506 Studničný, "
  +"link": "https://apk.kobera-nad.sk/aelocation?location=1"
}

however, when i call dd($this) at first line of toArray() method, it's 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: []
}

拜托,我该如何传递数据?

谢谢。

评论