Reputation: 161
I want to send post title and body from database to all user in email and emails are sent, but the default text is displayed in the email. title from database not sent in email.
Notification Class
class RepliedToThread extends Notification
{
use Queueable;
public $event;
public function __construct($event)
{
$this->event=$event;
}
public function via($notifiable)
{
return ['database','broadcast', 'mail'];
}
public function toDatabase($notifiable)
{
return[
'event'=>$this->event,
'user'=>auth()->user()
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'event'=>$this->event,
'user'=>auth()->user()
]);
}
public function toMail($notifiable)
{
return new MailMessage([
'data' => [
'event'=>$this->event,
'user'=>auth()->user()
]
]);
}
public function toArray($notifiable)
{
return [
//
];
}
}
Controller Code:
public function store(Request $request)
{
$this->validate($request, [
'title'=>'required',
'body'=>'required',
'color' =>'required',
'start_date' =>'required',
'end_date' =>'required',
]);
$event = new Event;
$event->title = $request->title;
$event->body = $request->body;
$event->color = $request->color;
$event->start_date = $request->start_date;
$event->end_date = $request->end_date;
$event->save();
$user = User::where('id', '!=', auth()->user()->id)->get();
\Notification::send($user,new RepliedToThread(Event::latest('id')->first()));
return redirect()->route('events.index');
}
Emails are sent, but the default text is displayed in the email and post title from database not sent in email.
I want to send post title and body from database to all user in email.
Upvotes: 0
Views: 3051
Reputation: 2945
Laravel Notification
In Laravel Notification:
public function __construct(array $event)
{
$this->event = $event;
}
public function toMail($event)
{
return (new MailMessage)
->line($this->event->title)
->line($this->event->body);
}
In Controller:
Notification::send($user,new RepliedToThread($event));
Notification layout template store here /vendor/laravel/framework/src/Illuminate/Notifications/resources/views
Upvotes: 1