The Problem
I would like to automatically add created_by
and modified_by
fields to every insert/update to a database table in Laravel 4, regardless of whether I am using Eloquent or Query Builder. However, not all my tables have these fields so any solution will have to check these columns exist before adding.
Attempted Solution
I have extended the Illuminate\Database\Eloquent\Model
class and written an overwrite method save()
in order to add some additional meta data fields for every record that is saved.
This is fine except that if I perform an insert using the Query Builder then this is bypassed. Looking at the Model
class it appears that the database operations are actually done using the query builder.
I have had a look at the Illuminate\Database\Query\Builder
model and it looks like I could probably write overwrite methods for insert()
and update()
.
Is this a sensible way to go about performing some task for every insert/update or will I run into trouble later down the line?