Notifications / Mail with Laravel [closed]

1

I'm having trouble firing notifications with Laravel 5.4, I already followed the documentation steps, but I did not succeed.

Follow 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 for E-mail firing.

  

\ LaraShop \ Users \ Emails \ MailMessage

<?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 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()
    ]);
}
    
asked by anonymous 22.08.2017 / 01:00

1 answer

1

For the notification to be written to the database, in the return array of its via method, it must have the database item.

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 notifications table with the commands:

php artisan notifications:table

php artisan migrate

Source: Documentation

    
23.08.2017 / 20:09