Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to Store Model Observers (Events) in Laravel 5

Just trying out Laravel events and observers for the first time.

http://laravel.com/docs/5.0/eloquent#model-observers

I see in some old postings about Laravel 4 a good place to store them was just in app/models/observers. However in Laravel 5 since the folder structure has changed a bit I'm wondering where is a good place to store them?

Should I be storing them in the Events folder?

like image 627
Rob Avatar asked Jan 25 '26 04:01

Rob


1 Answers

This is one of those questions that may have a wrong answer but multiple right answers. I would suggest we re-visit the basics a bit.

I believe the folder structure of a web app should be intuitive. Someone looking at the code for the first time should very quickly understand what to find where. So, here is what I would suggest.

Since Laravel 5 has the models in the app folder, you can namespace them to a Models folder. Your model code will then be housed at app/Models. Similarly, you can keep your model observers at app/Models/Observers and namespace them accordingly.

You can also save them in the Events folder or a separate Observers folder in the app folder but as I have said, the above is more intuitive.

like image 88
Noman Ur Rehman Avatar answered Jan 27 '26 18:01

Noman Ur Rehman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!