Trigger to update field on every CRUD operation laravel 5 Query Builder

I have a situation. I am working on some backend applications where I need to keep track of the id of the last user who made some changes to the data (database CRUD operation).

I use Query Builder for this (due to some issues, but yes, I followed building Query for app, and eloquent is just for user table). This is a website where data is more important and we want to track the last user who changed a database change. I have about 20 tables where the data is changing.

Now it needs me to look for some kind of laravel trigger where I will be programming the code in one place and it will update for every CRUD. Therefore, whenever the data changes, this trigger updates the user ID. I don't want to code every CRUD call, so there must be some kind of trigger that can help us here.

How I haven't used Eloquent. I know I can use the parent :: boot method on my parent model and can distribute others using that model.

So is there any other way to create Query. So there is something that might help. Any help guys?

I am using laravel 5

+3


source to share


2 answers


Laravel fires events for db changes, I believe it illuminate.query

, but I haven't used that, so I might be wrong.



0


source


L5 automates events in CRUD actions automatically. Even you can hook at different moments of the action, you get this out of the box in L5:

  • creature,
  • created,
  • update,
  • updated,
  • preservation,
  • saved,
  • deletion,
  • deleted,
  • recovery,
  • recovering.

See Model Events and Model Observers

To listen for events for the query builder, you can hook into the event iluminte.query

.



Event::listen('illuminate.query', function($query, $params, $time, $conn) 
{ 
    dd(array($query, $params, $time, $conn));
});

      

You can also listen like this

DB::listen(function($squery, $params, $time)
{
   dd(array($query, $params, $time));
});

      

0


source







All Articles