Dayat Fadila
Dayat Fadila

Reputation: 21

Laravel 5, log DB::table insert, update , delete events

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.

Upvotes: 2

Views: 2808

Answers (2)

Doom5
Doom5

Reputation: 858

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
    });
}

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

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
        });
    }

Upvotes: 0

Related Questions