1

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": "info, "
  +"link": "https://app.com/query?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: []
}

Please, how can I pass the data through?

Thank you.

5
  • I am not able to understand what's your problem is. Can you write more code pls? Commented May 23, 2020 at 14:09
  • in __construct i get data passed as $conSite, i want to store them in the model and call use them in toArray(), but i cannot access them there Commented May 23, 2020 at 14:25
  • where are the definitions of CSid, outdatedAes and link in the class? Commented May 23, 2020 at 14:27
  • $conSite = collect(); $conSite->id = $mcs->id; $conSite->link = "www.website.com/" . $mcs->id; $conSite->outdatedAes = "string" it's just integer, string, string Commented May 23, 2020 at 14:30
  • I am not talking about assigning value to them. The 3 fields are members of the class. You have to define them. Commented May 23, 2020 at 14:34

1 Answer 1

1

First of all you have to define the members in the class:

<?php

class OutdatedAELocation extends Notification implements ShouldQueue
{
    use Queueable;

    // HERE you define the members
    var $CSid;
    var $outdatedAes;
    var $link;

    // ...
}

After that try dd($conSite); at the start of the constructor to see if you pass a full object to the class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.