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

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 插入操作日志到数据库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Laravel监听数据库访问,打印SQL的例子

    增加一个helper函数 if ( ! function_exists('sql_dump')) { function sql_dump() { \DB::listen(function ($sql) { $i = 0; $bindings = $sql->bindings; $rawSql = preg_replace_callback('/\?/', function ($matches) use ($bindings, &$i) { $item = isset($bindings[$i

  • Laravel获取所有的数据库表及结构的方法

    遇到一个需求,需要修改数据库中所有包含email的字段的表,要把里面的长度改为128位.Laravel获取所有的表,然后循环判断表里面有没有email这个字段. 代码如下: use Illuminate\Support\Facades\Schema; use DB; public function getDatabaseColumns() { $tables = DB::select('show tables'); $tables = array_column($tables, 'Tables_

  • laravel 获取某个查询的查询SQL语句方法

    如下所示: DB::connection()->enableQueryLog();#开启执行日志 $count = DB::table('test') //执行查询 ->whereNull('deleted_at') ->where('id', '=', 3) ->where('Name', '=', '测试') ->count(); print_r(DB::getQueryLog()); //获取查询语句.参数和执行时间 Array ( [0] => Array (

  • laravel实现查询最后执行的一条sql语句的方法

    代码: DB::connection()->enableQueryLog(); $query = DB::table('test')->orderBy('id', 'desc')->get();//需要执行的SQL语句 echo '<pre>'; print_r(DB::getQueryLog()); 执行结果为: Array ( [0] => Array ( [query] => select count(*) as aggregate from `sj_rea

  • Laravel使用原生sql语句并调用的方法

    有一些sql语句比较复杂,用构造器还不如直接用sql来的方便,我们在laravel中使用原生语句,首先要在开头use DB,然后: $arr = DB::select("select id,sum(parents+1) as total_people from orders where game_id=6 and pay_status=1 and hotel_id=5"); 接下来,我们程序里怎么获取到查询到的值呢? 查询到的是一个数组,但是里面的内容属于对象,我们要这么调用: $ar

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

    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

  • SQLSERVER简单创建DBLINK操作远程服务器数据库的方法

    本文实例讲述了SQLSERVER简单创建DBLINK操作远程服务器数据库的方法.分享给大家供大家参考,具体如下: --配置SQLSERVER数据库的DBLINK exec sp_addlinkedserver @server='WAS_SMS',@srvproduct='',@provider='SQLOLEDB',@datasrc='10.131.20.100' exec sp_addlinkedsrvlogin 'WAS_SMS','false', NULL,'CustomSMS','Sql

  • C语言连接并操作Sedna XML数据库的方法

    本文实例讲述了C语言连接并操作Sedna XML数据库的方法.分享给大家供大家参考.具体如下: #include "libsedna.h" #include "stdio.h" int handle_error(SednaConnection* conn, const char* op, int close_connection) { printf("%s failed: \n%s\n", op, SEgetLastErrorMsg(conn))

  • Java连接并操作Sedna XML数据库的方法

    本文实例讲述了Java连接并操作Sedna XML数据库的方法.分享给大家供大家参考.具体分析如下: Sedna 是一个原生的XML数据库,提供了全功能的核心数据库服务,包括持久化存储.ACID事务.索引.安全.热备.UTF8等.实现了 W3C XQuery 规范,支持全文搜索以及节点级别的更新操作. import ru.ispras.sedna.driver.*; public class SednaClient { public static void main(String args[])

  • Android批量插入数据到SQLite数据库的方法

    Android中在sqlite插入数据的时候默认一条语句就是一个事务,因此如果存在上万条数据插入的话,那就需要执行上万次插入操作,操作速度可想而知.因此在Android中插入数据时,使用批量插入的方式可以大大提高插入速度. 有时需要把一些数据内置到应用中,常用的有以下几种方式: 1.使用db.execSQL(sql) 这里是把要插入的数据拼接成可执行的sql语句,然后调用db.execSQL(sql)方法执行插入. public void inertOrUpdateDateBatch(List<

  • Laravel框架实现利用中间件进行操作日志记录功能

    本文实例讲述了Laravel框架实现利用中间件进行操作日志记录功能.分享给大家供大家参考,具体如下: 利用中间件进行操作日志记录过程: 1.创建中间件 php artisan make:middleware AdminOperationLog 2.生成了文件./app/Http/Middleware/AdminOperationLog.php 代码如下: <?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\R

  • Laravel 框架中使用 MongoDB 数据库的操作

    1.先确定好自己使用的哪个版本的 Laravel 框架,再决定 composer 哪一个的 MongoDB,我使用的是 Laravel 8 所以我 composer 了 3.8 的MongoDb 2.执行 composer 命令,进行下载,我是用的是第二个命令 composer require jenssegers/mongodb ^3.8 -vvv composer require jenssegers/mongodb:3.8 --ignore-platform-reqs 3.这个时候可能会报

  • Laravel 5.2 文档 数据库 —— 起步介绍

    1.简介 Laravel 让连接多种数据库以及对数据库进行查询变得非常简单,不论使用原生 SQL.还是查询构建器,还是 Eloquent ORM.目前,Laravel 支持四种类型的数据库系统: MySQL Postgres SQLite SQL Server 配置 Laravel 让连接数据库和运行查询都变得非常简单.应用的数据库配置位于config/database.php.在该文件中你可以定义所有的数据库连接,并指定哪个连接是默认连接.该文件中提供了所有支持数据库系统的配置示例. 默认情况

  • Laravel框架使用monolog_mysql实现将系统日志信息保存到mysql数据库的方法

    本文实例讲述了Laravel框架使用monolog_mysql实现将系统日志信息保存到mysql数据库的方法.分享给大家供大家参考,具体如下: Laravel中使用monolog_mysql将系统日志信息保存到mysql数据库 源码参考: https://github.com/markhilton/monolog-mysql 一.安装Installation 在文件根目录: composer require markhilton/monolog-mysql 发现composer.json文件里的

  • SQL Server 2008及更高版本数据库恢复方法之日志尾部备份

    经常看到有人误删数据,或者误操作,特别是update和delete的时候没有加where,然后就喊爹喊娘了.人非圣贤孰能无过,做错可以理解,但不能纵容,这个以后再说,现在先来解决问题. 遇到这种情况,一般都是没有做备份,不然也不会来发问了.首先要冷静,否则会有更大的灾难.直到你放弃. 解决方法: 对于这类问题,主要是找回误操作之前的数据,在2008之前,有个很出名的工具Log Exploer,听说还挺好用的,这个网上大把教程,这里就不多说了.但是唯一遗憾的是,不支持2008及更高版本,这时除了其

随机推荐