What is the best way to use Notifications in Laravel 5.3

Asked

Viewed 984 times

1

I’m using the event Registered of Laravel himself (Illuminate Auth Events Registered) to fire a Listener I created to fire a welcome email.

See how the Listener turned out:

<?php

namespace App\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

// instância do evento registered do próprio Laravel
use Illuminate\Auth\Events\Registered;
use Illuminate\Notifications\Notifiable;
use App\Notifications\EnviarEmailBoasVindasNotification;

class EnviarEmailBoasVindas
{
    use Notifiable;
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  NovoUsuario  $event
     * @return void
     */
    public function handle(Registered $event)
    {
        // No "envelope" $event podemos acessar a instância User assim $event->user
        $event->user->notify(new EnviarEmailBoasVindasNotification($event));
    }
}

My doubt is on account of "use Illuminate Notifications Notifiable" and by the occurrence "use Notifiable" already within the class. What is the use of these? From what I have seen to remove both will continue to function normally.

  • There’s something that doesn’t match your code, because, these namespace and traits may not be required there, but may be required in the class within the method notify. You followed the documentation I did something else?

  • Everything correct, at first I followed the documentation as it should be. See if I’m correct, $Event has an instance of the User model, within this model I have the occurrence use Illuminate Notifications Notifiable;, this would be the reason why I don’t need to have a new occurrence in the above file?

  • Where do you call EnviarEmailBoasVindas?

  • I have an Event that calls this Liener (Send Emailboasvindas) there.

  • 1

    I would make a notification and a observers? I think it becomes more logical.

  • In fact Virgilio, in this case it is better to use Observers. I did not know this option. However, I believe that in the above case, my understanding is correct in imagining that I do not need to instantiate the notification class again since it is instantiated in the User model

  • I could not understand what you did because I have done it and I have examples and need yes, from what I could perceive or imagine there are two classes calling each other! good is assumption. but, in your case it is better an Observer with method created with notification!

Show 2 more comments

1 answer

2


First, you need to understand what you are using when making use of use Notifiable.

This is about a trait. A trait in PHP is a code reuse engine that aims to solve methods inheritance problems.

With a trait, you can write something as if it were "a piece" of the class and inherit in several other classes.

That’s what Laravel did. What you’re wearing is a trait which has some methods to facilitate the use of its notification.

The trait Notifiable inherits two other traits: HasDatabaseNotifications and RoutesNotifications. They have several methods.

For you to know the difference, just do so:

print_r(get_class_methods(new App\Listeners\EnviarEmailBoasVindas));

This will display all methods in your class. You debug using the use Notificable, then debug without. You will notice that methods are added in your class.

Completion

If you are not using any of the methods of Notifiable, really no need to inherit it. Use if you need to.

There are cases where traits are placed to maintain the default behavior of the Framework, as in the case of authentication traits (then I put an example for you to better understand).

  • Very good, beautiful explanation. Now I understand.

Browser other questions tagged

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