6

I am creating laravel 5.3 database notifications.I have created notifications as per video published on https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/10 , Now i want to add custom fields to the notification table as per my requirements. Please help me how to pass custom data to notification and access it.

4
  • 1
    Having similar problem, anyone please help Commented Apr 7, 2017 at 9:45
  • 1
    found any solution ? Commented Jun 16, 2017 at 13:31
  • me too, seems that notifications are too young, or not enough well documented for use it effectively Commented Jul 4, 2017 at 12:39
  • @DivyaBhalodiya stackoverflow.com/a/43658694/69537 Commented Mar 4, 2018 at 19:22

1 Answer 1

-1

When I needed to put custom fields to Notification, I'd just put on data field, as it is a Json field, works perfectly. Like this:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

class TaskNotification extends Notification
{
    use Queueable;

    private $message;

    /**
     * @param String $message
     */
    public function __construct($message=false)
    {
        if ($message)
            $this->message = $message;
    }

    /**
     * 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)
    {
        return [
            'message' => $this->message,
            'link' => route('mymodel.show'),
            'task'=> 1, // This is one variable which I've created
            'done'=> 0 // This is one variable which I've created
        ];
    }
}
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.