2

I am developing Laravel 5 app. In Which I want to log DB::table insert, update and deleted event with all New or changed(in case DB::table is being updated) DB::table Fields . I want simple reusable solution without writing too much of a code.

Dayat Fadila
  • 21
  • 1
  • 3

2 Answers2

0

Simple solution is to use Eloquent Events.

You can bind events for all models you want globally using Service Provider:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        User::creating(function ($user) {
            // Do logging
        });
    }
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • thank,but i'm using DB::table for insert,update,delete data. in eloquent event it success , but how it in DB::table? – Dayat Fadila Jun 07 '16 at 05:47
  • @DayatFadila, almost same principle. Doom5 gave you a link to Query Events: https://laravel.com/docs/5.2/database#listening-for-query-events – Alexey Mezenin Jun 07 '16 at 06:05
0

You can use DB::listen() as stated in here.

// in your AppServiceProvider
public function boot()
{
    DB::listen(function ($query) {
        // $query->sql
        // $query->bindings
        // $query->time
    });
}
Doom5
  • 857
  • 9
  • 18