mercredi 2 décembre 2015

Is it possible to create an observer to observe all models implementing an interface in Laravel 5.1?

I have an audit class which extends the Eloquent Model...

class Audit extends Model {
}

I have an auditable interface...

interface IAuditiable {
    public function audit();
}

I have a trait which fulfils the interface and defines the relation between the model and the audit...

trait Auditable {
    public function audit(){
        return $this->hasMany('Audit');
    }
}

I have a model which extends the Eloquent Model implements the interface and uses the trait...

class Post extends Model implements IAuditable {
    use Auditable;
}

I'd like to add the functionality in there to create or update an audit whenever the Post model is created or updated. I've solved this by registering an observer on the Post which would catch the 'saved' event and add a new audit.

However, there will eventually be many models using implementing IAuditable and using the Auditable trait.

So, my question is, is it possible to implement an observer which would pick up all 'saved' events for any model which implements the IAuditable interface in Laravel 5.1?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire