Laravel Model Observers

Laravel Model Observers

This article discuss the Laravel Model Observers. Observers help you observe any change on your Eloquent Model, for example you save a record, update a record etc. and Observer will observe that change and you can perform any action upon that change.

Laravel Model Observers

You can read the official documentation about observers here.

For Example We have a store function that saves an article like,

public function store(Request $request)
{
    $employee = Article::create($request->all());
    return redirect()->route('article.index');
}

We can also put the change to the log file here as,

public function store(Request $request)
{
    $employee = Article::create($request->all());
    Log::info('Article is saved');
    return redirect()->route('article.index');
}

According to the MVC architecture and design patterns, it is pretty bad to perform all the actions in the controller and the controller should be as slim and shorter as possible and the logic should be somewhere else like the service classes etc. The controller should only be responsible for redirecting things if possible.

The better way to this is to use observers which will observe the change on the Article model and and you can do whatever you want like send notification to the user, save to log file as we will do here etc.

Observer can be create by the following command

php artisan make:observer ArticleObserver --model=Article

And it will create a file in App/Obervers as ArticleObserver.php and will look like this,

<?php
namespace App\Observers;
use App\Models\Article;
class ArticleObserver
{
    /**
     * Handle the User "created" event.
     *
     * @param  \App\Models\User  $user
     * @return void
     */
    public function created(Article $article)
    {
        //
    }
    /**
     * Handle the User "updated" event.
     *
     * @param  \App\Models\User  $user
     * @return void
     */
    public function updated(Article $article)
    {
        //
    }
    /**
     * Handle the User "deleted" event.
     *
     * @param  \App\Models\User  $user
     * @return void
     */
    public function deleted(Article $article)
    {
        //
    }
    /**
     * Handle the User "forceDeleted" event.
     *
     * @param  \App\Models\User  $user
     * @return void
     */
    public function forceDeleted(Article $article)
    {
        //
    }
}

We will have to register this observer in the boot method of AppServiceProvider as,

use App\Models\Article;
use App\Observers\ArticleObserver;
/**
 * Register any events for your application.
 *
 * @return void
 */
public function boot()
{
    Article::observe(ArticleObserver::class);
}

write the code of your choice in the created method of the observer which will be performed after the Article will be saved each time.

    /**
     * Handle the User "created" event.
     *
     * @param  \App\Models\User  $user
     * @return void
     */
    public function created(Article $article)
    {
        Log::info('Article is Saved!!');
    }

And whenever the Article will be saved, the ArticleObserver will observe the change and will log the Article is Saved!! to the Log file.

Now if we Want to change the record before it is saved, we can create a method like creating in the observer ,

    public function creating(Article $article)
    {
        $article->user_id = auth()->id();
    }

This will be changes before it will be saved and in the user_id of Article model will be having the auth user id.

The above code can be placed in various place like mutators, can also be in the controller but it can also can be written in the observers.

Follow us TwitterFacebookLinkedIn

Open Source Listing

Previous Post
Next Post

Leave a Reply