Eloquent Observers or Events Listeners? Which is Better?

preview_player
Показать описание
Want to send an email notification whenever some record is added or changed? There are two ways. And, broadly speaking, we are talking about some event happening, and another class "reacting" to that event.

From official docs:

- - - - -
Рекомендации по теме
Комментарии
Автор

I've always preferred firing events. It seems to me like less of a mystery when tracking issues and other stuffs.

jpware
Автор

you can send the event through the observer, this would be a good approach because its using single responsibility principle

Pb-pjft
Автор

And again a great explanation. Thank you

JohnnyBigodes
Автор

your videos have helped me a lot


thanks!

cnavrow
Автор

you may also try DB::listen and put it at constructor of the controller, in that method in logs all queries not only models, you have the query string and parameters array,

filter non query scripts, filter table modified and trigger your functions

I use this to all my controllers to log non query statements

deecee
Автор

Great explanation about the difference. Thanks

laraleff
Автор

Hm, the first option - observer has in it's constructor fetching admins via database?? So every time an observer is imported or initiated there will be db query..

milospetkovic
Автор

Really good video as always. Thanks A LOT! Keep pushing, keep going!

belce
Автор

Great video. Thanks for sharing it with us . Have a good day

claudiobrt
Автор

0 dislikes ttt) first seen at Youtube. Q - means quality! Best

dmdk
Автор

Смотрю Ваши уроки на английском языке, хотя я русский. Очень понятно и по делу. Большое спасибо)

zgpcmdy
Автор

Hi, thanks for the good content. I don't really get the purpose of firing an event the way you recommend, rather than just calling some service to perform that action. What benefit is there to use fire an event over using some service?

I understand that events are binded to certain actions, and usually happen behind the scenes, so are not declared in code and that is the benefit. For example you want a certain action to happen whenever a status is updated, regardless of where in the code its done from, its good to have an event, because if another developer comes and updates a status somewhere, automatically that event will get fired. But as you said the drawback is it makes the code harder to read and follow.

SGDominicana
Автор

What is the difference between event(new Event::class) and Event::dispatch()

simbarasheandrea
Автор

Hi, what if I would like to notify other users that were appointed as approvers for project?

ngebolangria
Автор

How to stop model from being observed in specific situations?
For example: I have set user_id in observers, but I have an admin user which requires all user_id s and need the observer to be off.

renwar
Автор

Is observer run everytime? Could we cancel updated the model by checking condition?

bboydarknesz
Автор

Great explanation. But here comes a question, Lets extend your use case. 1) Project::create 2) notify() here we are sending email. How to make it always working, that everytime New project is created it sends email? Lets suppose Project is created successfully but EMAIL could not be sent for some condition (server down, unavailable, error). How to catch this scenario? Do we need to delete record from db and try again. Or resned email. Plz elaborate your suggestions. Thanks. Respect from Pakistan 🇵🇰

nabeelyousafpasha
Автор

I did notice that if you use events and listeners in a dispatched job then it doesn't work. Is there a reason for this?

brianngetich
Автор

If I want to do an action base on the result of something (like if a user registration was successfull or failed). Should I make two events or just one event with status of the action passed by param?

JoseDlucca
Автор

how I receive an email when something changes on the database Like the expired DateTime
In my case there is a Building table has a column check_at (timestamp)
I need when check_at < now() send email to me or receive an notification

Thank you

alila