Logging user actions in laravel

后端 未结 2 1660
心在旅途
心在旅途 2021-02-10 04:55

I\'m trying to log all actions that users do (login / logout / CRUD) to a logs table in my database, and from what I\'ve seen events look to be the right way to do this.

2条回答
  •  面向向阳花
    2021-02-10 05:38

    you can make helper function like that

    function LogSystem($table,$action,$custom=null)
    {
        $table::$action(function ($service) use ($action,$custom){
            if( ! is_null($custom))
            {
                $url='/panel/'.$custom.'/'.$service->id.'/edit';
            }
            else
            {
                $url=\Illuminate\Support\Facades\Request::fullUrl();
            }
            \Illuminate\Support\Facades\DB::table('log_activity')->insert([
                'subject' => $action.'::=='.$service->title,
                'url' => $url,
                'method' => \Illuminate\Support\Facades\Request::method(),
                'agent' => \Illuminate\Support\Facades\Request::header('user-agent'),
                'ip' => \Illuminate\Support\Facades\Request::ip(),
                'created_at'=>\Carbon\Carbon::now(),
                'user_id' => auth('admin')->check() ? auth('admin')->user()->id : 1,
            ]);
    //
        });
    }
    

    Then Go to serviceproviders

    LogSystem('\App\Services','created','services');
    

    you Can call this for all croud system for any module you want

提交回复
热议问题