Introducing Laravel Model Events
If you have used Laravel for medium and large scale projects, you may have come across a situation where you want to take some action while Eloquent is processing. You can do it manually but you don’t know that Laravel Eloquent provides a simple way to add in the model while the model is completing or has completed some actions. For example, you have a model called Post
and you want it to automatically add slug from the title while you are performing the operation to add a new post. You can do this by setting your model when a new post is added with a saving
event like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | namespace App; use IlluminateDatabaseEloquentModel; class Post extends Model { protected $table = 'posts'; protected $fillable = ['title', 'slug', 'content']; protected static function boot() { parent::boot(); static::saving(function ($model) { $model->slug = str_slug($model->title); }); } } |
Eloquent provides the following events when we manipulate the model:
- 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.
Laravel Model Observers
Imagine that your application grows and grows day by day you will be very tired or have to handle a lot after each manipulation of the model. But if you use Observer then everything will be very simple . Using model observers, you can group all your events into one class. All method names in the observer class will automatically reflect
on each of the events listed above. You can create an observer model class using php artisan
like this:
1 2 | php artisan make:observer PostObserver --model=Post |
The above command will create a new class in the project directory like this app/Observers
:
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) { // } } |
Now you need to add the newly created class above to the AppServiceProvider
file in the boot()
to tell the Post
model that every time any event (as I have listed above) affects the Post
model, Let’s implement the methods defined in observe as PostObserver
like this :
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); } } |
Then as mentioned above we want to add the slug every time we create a new post, we will do it with the on saving
method as follows:
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 "saving" event. * * @param AppPost $post * @return void */ public function saving(Post $post) { $post->slug = str_slug($post->title); } } |
Now every time you create a new post it will automatically get the title converted to slug and add it to the attribute slug
very well, right?
Conclude
Above is an article on how to use observer with model, in fact, when we code a large project, when using observer, it is actually a very optimal and very good time-saving way. you succeed