ahoi
272
3
Laravel

Send mail on Model created

Posted 7 months ago by ahoi

Hello everybody,

I am new to Laravel events and notifications. I'd like to send a simple mail, once a model is created.

Info: I am using Laravel 5.7

 Message::create([
            'user_id'   => auth()->user()->id,
            'body'      => $request->data['message'],
        ]);

Is it possible to fetch this create event and trigger a notification which sends a mail via toMail?

I followed this one: https://sujipthapa.co/blog/automatically-posting-to-facebook-page-via-laravel-notifications

This is my model:

<?php

namespace App;

use Cmgmyr\Messenger\Models\Message as BaseMessage;
use Illuminate\Notifications\Notifiable;

class Message extends BaseMessage
{
    use Notifiable;
}

This is my listener:

<?php

namespace App\Listeners;

use App\Notifications\MessageSaved;

/**
 * Class MessageEventSubscriber
 * @package App\Listeners
 */
class MessageEventSubscriber
{
    
    /**
     * Handle message creating events.
     *
     * @param $message
     */
    public function onSaved($message)
    {
        $message->notify(new MessageSaved());
    }
    
    /**
     * @param $events
     */
    public function subscribe($events)
    {
        $events->listen(
            'eloquent.saved: App\Message',
            'App\Listeners\[email protected]'
        );
    }
}

This is my notification:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class MessageSaved extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

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

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

and this is my EventServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen
        = [
            'App\Events\Event'               => [
                'App\Listeners\EventListener',
            ],
            Registered::class                => [
                SendEmailVerificationNotification::class,
            ],
        ];
    
    protected $subscribe = [
        'App\Listeners\MessageEventSubscriber',
    ];
    
    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();
        
        //
    }
}

Unfortunately the notification is not being triggered this way.

Your help is highly appreciated :-)

Please sign in or create an account to participate in this conversation.