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.
Asked
Active
Viewed 590 times
6
-
1Having similar problem, anyone please help – Divya Bhaloidiya Apr 07 '17 at 09:45
-
1found any solution ? – Awais Usmani Jun 16 '17 at 13:31
-
me too, seems that notifications are too young, or not enough well documented for use it effectively – Luca C. Jul 04 '17 at 12:39
-
@DivyaBhalodiya https://stackoverflow.com/a/43658694/69537 – B Faley Mar 04 '18 at 19:22
1 Answers
-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
];
}
}

RafaelQm
- 132
- 1
- 8