In case you don´t use the Laravels timestamp mechanism

Micha(el) Bladowski
1 min readApr 18, 2021
Photo by Ali Hajian on Unsplash

Just a short one…

In case your columns have a unique prefix for each table, for example:

products:

  • pro_id
  • pro_name
  • pro_db_created
  • pro_db_changed
  • and so on…

and in case you prefer triggers, because you want timestamps ALWAYS be set correctly and not only if Laravel touches your data, in that case,
this little helper might help you :)

It searches all your tables missing triggers and looks for “xxx_db_created” and “xxx_db_changed” — the prefix is taken by the primary key, in this example
it’s “pro_id” so “pro” is our prefix.

If “pro_db_created/changed” exists, triggers will be created for:

  • before update
  • before insert

with “SET NEW.xxx_db_changed = NOW()”
and “SET NEW.xxx_db_created = NOW()”.

No doubt, the Laravel Eloquent stuff is great!
But more important — for me — is that fact, if anything or anybody else is touching my data, I need and want a correct timestamp — so for me, triggers are the most reliable option here.

--

--