1

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.

Devin Dixon
  • 11,553
  • 24
  • 86
  • 167

1 Answers1

0

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.

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204