Answer the question
In order to leave comments, you need to log in
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 EventServiceProviderpublic 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
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 questionAsk a Question
731 491 924 answers to any question