laravel中插入日志到数据库怎样做,方法是什么
Admin 2022-07-01 群英技术资讯 572 次浏览
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, ];
大功告成…
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
备忘录模式是一种行为型模式,它在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样可以在以后把该对象的状态恢复到之前保存的状态。主...
命令模式:命令模式(CommandPattern):将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化;对请求排队或者记录请求日志,以及支持可撤销的操作。命令模式是一种对象行为型模式,其别名为动作(Action)模式或事务(Transaction)模式。模式动机:在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,
php是一门适用于web开发的动态语言,它可以说是一个用C语言实现的包含大量组件的软件框架。那么,php是如何工作的呢?它的生命周期是怎么样的呢?
php中get_object_vars()在数组的使用:1、获取$object对象中的属性,组成一个数组;2、语法,get_object_var($object);3、使用注意,缺点,只转一维,不会递归。
什么是PHP超级全局变量?超级全局变量是在全部作用域中始终可用的内置变量。超级全局变量在PHP 4.1.0之后被启用,是PHP系统中自带的变量,在一个脚本的全部作用域中都可用
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008