1. Introduction
If you are and have used Laravel for a medium to large project, you may have come across a situation where you want to perform some action in the Eloquent model you are dealing with. Laravel Eloquent provides a convenient way to add your own actions while the model is completing or has completed some actions.
Suppose you have a series of actions that take place one after another and depend on each other for example: when you delete a post, you also want to delete all its comments, or when you register successfully an account will automatically send emails to subscribers, … Although you can write the whole logic of that code in the Controller. But today I would like to introduce to you about Observer Events . First we will learn about Eloquent Model Events offline!
2. Laravel Model Events
Eloquent has provided events for you to hook at some point in its life cycle:
- retrieved : after a record has been retrieved from the database.
- creating : before a record has been created
- created : after a record has been created
- updating : before a record is updated.
- updated : after a record has been updated.
- saving : before a record is saved (created or updated)
- saved : after the record has been saved (created or updated).
- deleting : before a record is deleted or soft deleted.
- deleted : after a record has been deleted or soft deleted.
- restoring : before the softly deleted record will be restored.
- restored : after the soft deleted record has been restored.
3. Laravel Model Observers
As the example above, suppose you want to delete 1 post and delete all the comments of that post. Although we can handle it in the main PostController, but so it looks quite confusing code, right? Thankfully, Laravel has provided us with Observer Events, which can hook at different times during a post’s life cycle.
3.2 Create observer
You can easily create observer by running the command:
1 2 | php artisan make:observer NameObserver --model=NameModel |
In this example, I will create 1 PostObserver
1 2 | php artisan make:observer PostObserver --model=Post |
The PostObserver.php file will be created in the app / Observers directory and will have the following structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | namespace AppObservers; use AppPost; class PostObserver { /** * Handle the post "created" event. * * @param AppPost $post * @return void */ public function created(Post $post) { // } /** * Handle the post "updated" event. * * @param AppPost $post * @return void */ public function updated(Post $post) { // } /** * Handle the post "deleted" event. * * @param AppPost $post * @return void */ public function deleted(Post $post) { // } /** * Handle the post "restored" event. * * @param AppPost $post * @return void */ public function restored(Post $post) { // } /** * Handle the post "force deleted" event. * * @param AppPost $post * @return void */ public function forceDeleted(Post $post) { // } } |
3.2 Register observer
To use it, you must register it in the boot () function of AppServiceProvider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | namespace AppProviders; use AppPost; use AppObserversPostObserver; use IlluminateSupportServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { // } /** * Bootstrap any application services. * * @return void */ public function boot() { Post::observe(PostObserver::class); } } |
3.3 Example
I will take the VD when I delete a post, I will delete all comments of that post, here I will use the deleting function to hook that event. Here I use HasMany relationship
1 2 3 4 5 6 7 8 9 10 11 12 | namespace App; use IlluminateDatabaseEloquentModel; class Post extends Model { public function comments() { return $this->hasMany(Comment::class); } } |
Now I can add a deleting hook in PostObserver to delete all the comments of that post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | namespace AppObservers; use AppPost; class PostObserver { /** * Handle the post "deleting" event. * * @param AppPost $post * @return void */ public function deleting(Post $post) { $post->comments()->delete(); } } |
So you’ve successfully deleted all those comments
To understand more, please refer to here