php - Laravel revisionable models with one-to-many relationship -


is possible revisionable track changes one-to-many relationships? example:

model: user. model: posts. user model uses venturecraft\revisionable\revisionabletrait; , have hasmany relationship posts. if post added or updated, can tracked revisionable under user post belongs to?

thanks in advance

i able come something. it's not elegant solution, it'd great if me clean (especially unset bothering me).

my solution create duplicates in table model belongs to. don't know if wanted

the first thing need add nullable datetime revision_at column in appropriate table.

the trait pretty simple. make use of models boot() method register models updating event. fire whenever model update. that's need, since don't want revision first time creating model.

<?php  trait revisionabletrait {      public static function boot()     {         parent::boot();          static::updating(function( $model ){              // grab original model , unset id             // it, don't duplicate             // entries when create new model.              $attributes = $model->getoriginal();             unset( $attributes['id'] );              // next need add date revision              // created             $attributes['revision_at'] = new datetime();               $revision = new static($attributes);             $revision->save();          });       }  } 

the thing here grab original model before new fields assigned, unset id make sure don't create duplicate entry, set current time revision_at field , save model.

that's basically.

can tracked revisionable under user post belongs to?

this automatically done since new revision model still belongs respective user,

if want fine-tune create dedicated table revisions reference model stored. storing properties might little bit harder (maybe store them serialized).

another possible improvement modify getter methods of model in trait. example let all() return models no revisions. add withrevisions() method grab them too. can extract logic if take how laravel handles soft deletes. it's same.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -