实现laravel 插入操作日志到数据库的方法


Posted in PHP onOctober 11, 2019

1 . 创建一个中间件

执行: php artisan make:middleware OperationLog

2 . 在中间件中编写一个writeLog() 或者直接写在handle里面

<?php

namespace App\Http\Middleware;

use App\User;
use Closure;
use Illuminate\Support\Facades\Auth;

class OperationLog
{
  /**
   * Handle an incoming request.
   *
   * @param \Illuminate\Http\Request $request
   * @param \Closure $next
   * @return mixed
   */
  public function handle($request, Closure $next)
  {
    $input = $request->all(); //操作的内容
    $path = $request->path(); //操作的路由
    $method = $request->method(); //操作的方法
    $ip = $request->ip(); //操作的IP
    $usernum = $request->usernum; //操作人(要自己获取)
    self::writeLog($usernum,$input,$path,$method,$ip);

    return $next($request);
  }
  public function writeLog($usernum,$input,$path,$method,$ip){

    $user = User::where('usernum',$usernum)->first();

    if($user) {
      $user_id = $user->userid;
    }

    $log = new \App\Models\OperationLog();
    $log->setAttribute('user_id', $user_id);
    $log->setAttribute('path', $path);
    $log->setAttribute('method', $method);
    $log->setAttribute('ip', $ip);
    $log->setAttribute('input', json_encode($input, JSON_UNESCAPED_UNICODE));
    $log->save();
  }
}

3 .创建一个OperationLog模型(这里我放在Models文件夹下了)

执行 : php artisan make:model Models\OperationLog

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class OperationLog extends Model
{

  //定义表
  protected $table = "operation_log";

  //定义主键
  protected $primaryKey = "id";
}

4 . 将中间件注册到Kernel.php 文件

/**
 * The application's global HTTP middleware stack.
 *
 * 这些中间件是在对应用程序的每次请求中运行的
 *
 * @var array
 */
protected $middleware = [
    .......,
    .......,
    .......,
    \App\Http\Middleware\OperationLog::class,
  ];

大功告成…

以上这篇实现laravel 插入操作日志到数据库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
ajax php 实现写入数据库
Sep 02 PHP
PHP 网络开发详解之远程文件包含漏洞
Apr 25 PHP
比较时间段一与时间段二是否有交集的php函数
May 31 PHP
PHP中怎样保持SESSION不过期 原理及方案介绍
Aug 08 PHP
使用PHP导出Redis数据到另一个Redis中的代码
Mar 12 PHP
Codeigniter出现错误提示Error with CACHE directory的解决方案
Jun 12 PHP
smarty模板引擎从配置文件中获取数据的方法
Jan 22 PHP
PHP 5.3和PHP 5.4出现FastCGI Error解决方法
Feb 12 PHP
php微信公众平台开发之微信群发信息
Sep 13 PHP
PHP实现针对日期,月数,天数,周数,小时,分,秒等的加减运算示例【基于strtotime】
Apr 19 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
Feb 27 PHP
PHP copy函数使用案例代码解析
Sep 01 PHP
laravel 获取当前url的别名方法
Oct 11 #PHP
解决PHP curl或file_get_contents下载图片损坏或无法打开的问题
Oct 11 #PHP
解决PHP使用CURL发送GET请求时传递参数的问题
Oct 11 #PHP
ThinkPHP 5.1 跨域配置方法
Oct 11 #PHP
零基础php编程好学吗
Oct 11 #PHP
Laravel获取当前请求的控制器和方法以及中间件的例子
Oct 11 #PHP
PHP与SQL语句写一句话木马总结
Oct 11 #PHP
You might like
模拟SQLSERVER的两个函数:dateadd(),datediff()
2006/10/09 PHP
PHP 文件上传全攻略
2010/04/28 PHP
PHP实现读取一个1G的文件大小
2013/08/24 PHP
PHP中使用strpos函数实现屏蔽敏感关键字功能
2014/08/21 PHP
实例简介PHP的一些高级面向对象编程的特性
2015/11/27 PHP
在Z-Blog中运行代码[html][/html](纯JS版)
2007/03/25 Javascript
IE下JS读取xml文件示例代码
2013/08/05 Javascript
JavaScript避免代码的重复执行经验技巧分享
2014/04/17 Javascript
JavaScript中的函数声明和函数表达式区别浅析
2015/03/27 Javascript
jQuery实现点击小图片淡入淡出显示大图片特效
2015/09/09 Javascript
vuejs2.0实现分页组件使用$emit进行事件监听数据传递的方法
2017/02/22 Javascript
Javascript实现一朵从含苞到绽放的玫瑰
2019/03/30 Javascript
使用kbone解决Vue项目同时支持小程序问题
2019/11/08 Javascript
快速解决vue2+vue-cli3项目ie兼容的问题
2020/11/17 Vue.js
[07:31]DOTA2卡尔工作室 英雄介绍主宰篇
2013/06/25 DOTA
CentOS下使用yum安装python-pip失败的完美解决方法
2017/08/16 Python
python实现支付宝当面付(扫码支付)功能
2018/05/30 Python
Python中生成一个指定长度的随机字符串实现示例
2019/11/06 Python
在OpenCV里实现条码区域识别的方法示例
2019/12/04 Python
基于python读取.mat文件并取出信息
2019/12/16 Python
在tensorflow中实现屏蔽输出的log信息
2020/02/04 Python
Python flask框架实现浏览器点击自定义跳转页面
2020/06/04 Python
解决运行django程序出错问题 'str'object has no attribute'_meta'
2020/07/15 Python
Python实现邮件发送的详细设置方法(遇到问题)
2021/01/18 Python
pycharm配置QtDesigner的超详细方法
2021/01/25 Python
html5移动端价格输入键盘的实现
2019/09/16 HTML / CSS
舒适的豪华鞋:Taryn Rose
2018/05/03 全球购物
西雅图的买手店:Totokaelo
2019/10/19 全球购物
Java语言程序设计测试题判断题部分
2013/01/06 面试题
英文翻译的自我评价语句
2013/10/04 职场文书
仓库理货员岗位职责
2013/12/18 职场文书
人事主管岗位职责
2014/01/30 职场文书
客运企业隐患排查工作方案
2014/06/06 职场文书
本科生自荐信
2014/06/18 职场文书
2014年个人工作总结报告
2014/11/27 职场文书
办公室岗位职责范本
2015/04/11 职场文书