Y
Y
yellow_pus2022-04-04 22:49:22
Laravel
yellow_pus, 2022-04-04 22:49:22

Why doesn't observer work?

I have a UserObserver in which

public function created(User $user)
    {
        Mail::send(['text'=>'mail'],['name','test'], function ($message){
            $message->to('[email protected]','Laravel api test project')->subject('Вы создали аккаунт');
            $message->from('[email protected]','Laravel api test project');
        });
    }
Just as indicated in the documentation, I registered an observer in the EventServiceProvider
public function boot()
    {
        User::observe(NewUserRegistered::class);
    }
But despite this, when adding a new user, this observer does not work. Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2022-04-05
@yellow_pus

User::observe(UserObserver::class);
There should be an Observer class, not an Event(?).
Also, EventServiceProvider doesn't really fit logically. If you register an ObserverServiceProvider it will be better.
The general logic should be like this. The creation of the model is performed by the created observer, in the created event, to which the listener is subscribed, is initiated. but in the listener, mail is sent.
An event call can be initiated directly through the model without laying an observer through protected $events .
Still there is a way for absolutely addicts through eloquent.created event.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question