Laravel-auditing: Ability to tag audits

Created on 4 Aug 2017  路  13Comments  路  Source: owen-it/laravel-auditing

It would be nice if you could 'tag' the audits for models, that way you can group similar relations and pull down all the audits for that tag.

E.g. lets assume you have a Product model which has a prices relation for specifying varying different prices in different currencies, or a features relation specifying different features / configuration for the products - if you could get all the audits for the product in one go it would be cool.

Code examples:

$product = Product::find(1);
$audits = Audit::tagged('product-' . $product->getKey())->paginate(50);

To tag versions, you can add a function on your model, which get's called when an audit is created so it knows how to tag it:

class Product extends Model {

  public function tagAudit(Audit $audit)
  {
    return 'product-' . $this->getKey();
  }

Supporting multiple tags for a model could be an option, too.

enhancement

Most helpful comment

It looks like your not fulling understanding the implications of having to resort to those queries and how simple it is to overcome by categorising your audits on time of creation.

Yeah, you're right. I'll add this in the next minor version. Meanwhile, I'll keep this issue open.

Thanks, for the suggestion!

All 13 comments

Hi @garygreen,

The idea sounds good, but the examples you have don't really give it merit. What I mean by this is that the tag you're creating isn't adding value to the Audit record, it's just a variation of information we already have.

You can achieve the same functionality with:

Audit::where('auditable_type', Product::class)->andWhere('auditable_id', 1)->paginate(50);

Thoughts?

@quetzyg what if you have 5 different relations that you want to pull down the information for? So you have a products page and you want to display all the audits for that product, and it's relations.

At the moment, you would have to do:

Audit::where(function($query) {
  $query->where('auditable_type', Product::class)->andWhere('auditable_id', 1);
})->orWhere(function($query) {
  $query->where('auditable_type', ProductFeature::class)->andWhereIn('auditable_id', $product->features->modelKeys()));
})->orWhere(function($query) {
  $query->where('auditable_type', ProductPrice::class)->andWhereIn('auditable_id', $product->prices->modelKeys());
})
->paginate(50);

...for every relation, which means you might have to involuntarily load the relations just to get the audit information you need - this isn't too pretty. Having an indexed tag field is so much easier and more efficient.

I've created my own basic versioning system which uses the same technique and it's massively simplified the way we can pull down all related tagged-information for a given model, and sped things up.

Well, from what you suggest we have to make those ugly queries anyway, because you'll have create tags with the ProductFeature and ProductPrice ids each time you audit a Product. The only difference is that instead of querying when fetching Audit records, you do it on creation, or am I missing something?

It looks like your not fulling understanding the implications of having to resort to those queries and how simple it is to overcome by categorising your audits on time of creation. Doing that once is far more efficient rather than performing expensive and complicated queries everytime you want to display audits for all related models. Tagging data is not a foreign concept.

It looks like your not fulling understanding the implications of having to resort to those queries and how simple it is to overcome by categorising your audits on time of creation.

Yeah, you're right. I'll add this in the next minor version. Meanwhile, I'll keep this issue open.

Thanks, for the suggestion!

This issue has been addressed and the tagging feature will be available in version 5.0. This will be closed once the new version comes out.

V5 is gonna be tagged this tuesday during Laravel Lisbon // S01E01, closing issue and considering it solved.

We have the same problem and also solved it with all those queries like @garygreen mentioned, so it's great to see this feature was implemented, well done! :+1:

Is there no friendlier way to get audits by tag than something like:

Audit::where(DB::raw("FIND_IN_SET('TAG', tags)"), '>', 0)->get();

?

Doesn't something like this

Audit::where('tags', 'LIKE', '%foo%')->get();

work?

what if you have tags foo and food? :-)

anyway, what I wrote covers any possible case, I'm just saying maybe it would be nice for the library to provide an easy way to get audits by tag?

Well, you would have to explicitly look for what you want and use the exact string without %, then.

In regards to having an easy way to get audits by tag, you'll have to implement your own Query Scope to suit your needs, because I have no idea how each one is gonna use the tags column and it's hard to cater for everyone.

Eloquent should be flexible enough for anyone using this package.

Quiero utilizar el auditing tags, pero no entiendo bien como hacerlo, no tienen algun video donde expliquen como se hace

Was this page helpful?
0 / 5 - 0 ratings

Related issues

romoka picture romoka  路  5Comments

AdamKyle picture AdamKyle  路  7Comments

wyred picture wyred  路  7Comments

franciscojun picture franciscojun  路  8Comments

JeffBeltran picture JeffBeltran  路  3Comments