简体   繁体   中英

How Are Observers Called in Laravel4?

I am trying to create a logging system in Laravel 4 where anytime a model executes save, update or delete, I can log it into the database. But I am little confused after reading tutorial like this one:

https://bosnadev.com/2014/12/28/laravel-model-observers/

How are the observers called? How the model know when to fire them? I am confused on the implementation works.

Even in Laravel 4 you can use it's own observers for those functions:

<?php 

namespace App;

use Illuminate\Database\Eloquent\Model as Eloquent;

class BaseModel extends Eloquent
{
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->saving(function() {
            \Log::info('saving model '.get_class($this));
        });

        $this->updating(function() {
            \Log::info('updating model '.get_class($this));
        });

        $this->deleteing(function() {
            \Log::info('deleteing model '.get_class($this));
        });
    }
}

And you also have observers for saved, updated and deleted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM