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
-
1Having similar problem, anyone please helpDivya Bhaloidiya– Divya Bhaloidiya2017-04-07 09:45:08 +00:00Commented Apr 7, 2017 at 9:45
-
1found any solution ?Awais Usmani– Awais Usmani2017-06-16 13:31:01 +00:00Commented Jun 16, 2017 at 13:31
-
me too, seems that notifications are too young, or not enough well documented for use it effectivelyLuca C.– Luca C.2017-07-04 12:39:04 +00:00Commented Jul 4, 2017 at 12:39
-
@DivyaBhalodiya stackoverflow.com/a/43658694/69537Meysam– Meysam2018-03-04 19:22:07 +00:00Commented Mar 4, 2018 at 19:22
Add a comment
|
1 Answer
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
];
}
}