tag
204
1
Laravel

Logging Model Save Queries in Database

Posted 3 years ago by tag

I have the need to create an audit trail for what a user does in my application. I'm not really that worried about the selects, but really only the inserts/updates. I've created a BaseModel which all of my models extend. I've also created a table with an id, user_id, and a text column for the query. I want to save the query with the bindings that was executed during the save for each model. I know there is DB::enableQueryLog(); but I'm not sure that's exactly what I want, since I only want the query executed in the save. I'd love to not do this in my controllers where the save's occur, and instead do it in the base model, or listen to BaseModel::saving or BaseModel::save. Any ideas?

Please sign in or create an account to participate in this conversation.