September 29, 2019 · Cheatsheets Programming Laravel PHP MySQL ·

An Auditor for Laravel

I don't like working, especially on Sundays, but I get bored so I end up working. I'm not a masochist, or at least I think I'm not.

In the middle of the boredom, I made a new composer package for Laravel. Introducing the Laravel Auditor.

How did this come about?

Okay, a little background. At work, I'm working on two projects at the moment, and both projects have their own way of recording actions in the audit trail. One of the projects I'm handling is written in CodeIgniter, so we're steering clear of that mess for this post.

For the other project, it runs on Laravel. The code quality is better than most of the projects I've handled (creating a record doesn't send you to a function located only where god knows), so it was pretty straightforward. However, I spent a good number of hours working on the audit trail, because there were some weird things going on and our QA Engineer was having a field day of annoying me with bugs (just kidding, I'm not complaining, I know it's part of my job, please don't fire me).

I fiddled around with it and noticed that while the implementation was correct, it's not something you can easily manipulate. For instance, if I have to change a wording (for instance, changing "car" to "Car), I looked at the database to find that it wasn't there, but in some sort of collection processor segregated by if statements.

Of course, it's not entirely a bad thing, I just thought that maybe there was a way to avoid doing it like that. Fast forward to today, I got bored and decided to try writing a composer package that automatically handles the whole logging thing for you.

What's so special about Laravel Auditor?

Nothing. Absolutely nothing.

It's not me reinventing the wheel, it's not doing something so revolutionary. I just wrote code wrappers for most of the code I'm bound to write at work anyway, only this time I don't have to repeat myself because it's all packaged into a neat composer library I can include anytime.

Specifically, it "watches" a model using Laravel's observers and catches the changes. It then formats the changes into JSON, stripping sensitive information, using an easy-to-read structure (basically all the works) before it gets stored in the database.

For instance, an update action records the following data:

      "old":"2019-09-29 06:46:11",
      "new":"2019-09-29 06:46:14"

I made it use a JSON structure for two reasons:

Doesn't this make searching hard?


If you want to stick with fuzzy searching, just fuzzy-search the entire record JSON column. If you want to search for specific values, you could use whereJsonContains().

Laravel's whereJsonContains() method

Laravel has a whereJsonContains() method, which is useful for searching an exact key value. Something like User::whereJsonContains('first_name->old', 'value') works great if you want to do specific searches.

How do I start using Auditor?

I put up instructions and samples on the library's GitHub page. It should be easy enough to follow, since it's just like any other composer package.

It's totally free to use under the MIT Open Source License, but if it helped you in any way, feel free to buy me a drink (wink, wink).  Simply click the "donate bitcoin" button on the project page.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket