Notifications/Mail com Laravel

Asked

Viewed 693 times

1

I’m having trouble firing notifications with Laravel 5.4, I’ve already followed the steps of the documentation, but was unsuccessful.

Follows my code

\Larashop Users Notifications Messageuser

<?php namespace LaraShop\Users\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use LaraShop\Users\Emails\MailMessage as Mailable;

class MessageUser extends Notification
{
    use Queueable;

    private $message;
    private $user;
    public function __construct($message, $user)
    {
        //
        $this->message = $message;
        $this->user = $user;
    }
    public function via($notifiable)
    {
        return ['mail'];
    }
    public function toMail($notifiable)
    {
        return (new Mailable($this->message))->to($this->message->user->email);
    }    
    public function toArray($notifiable)
    {
        return json_encode($this->message->toArray());
    }
}

Class MailMessage to trigger the e-mail.

\Larashop Users Mailmessage Emails

<?php namespace LaraShop\Users\Emails;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use LaraShop\Users\Entities\Message;

class MailMessage extends Mailable
{
    use Queueable, SerializesModels;       
    private $message;   
    public function __construct(Message $message)
    {
        $this->message = $message;
    }
    public function build()
    {
        return $this->view('emails.default', [
            'email' => [
                'title' => 'Hello World!',
                'text' => 'Hello World!',
                'button' => [
                    'label' => 'Acessar',
                    'url' => route('page.users.messages.index'),
                ]
            ]
        ]);
    }
}

where I call the Facade of Notification of Laravel.

public function show(Request $request)
{
    $message = Message::find($request->item);
    \Notification::send($message, new MessageUser($message, $message->user));
    return view('users::pages.messages.show', [
        'message' => $message,
        'user' => \Auth::user()
    ]);
}
  • What’s the matter!?

  • Does not send the E-mail and also not saved in the bank the notification.

  • The way you’re doing is just sending an e-mail.

1 answer

1

To save the notification to the database, in the return array of your method via must have the item database.

For example:

/**
 * Get the notification's delivery channels.
 *
 * @param mixed $notifiable
 * @return array
 */
 public function via($notifiable)
 {
     return ['database', 'mail'];
 }

You also need to create the table notifications with the commands:

php artisan notifications:table

php artisan migrate

Source: Documentation

  • I had already created the table, still does not send.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.