laravel中插入日志到数据库怎样做,方法是什么
Admin 2022-07-01 群英技术资讯 975 次浏览
这篇文章给大家介绍了“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,
];
大功告成…
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
<?php//连接Memcache$mem=newMemcache;$mem->connect("localhost",11211)ordie("Couldnotconnect");//保存数据$mem->set('key1','Thisisfirstvalue',0,60);$val=$mem->get('key1');echo"Ge
这篇文章主要给大家分享CentOS7系统搭建LAMP的操作和如何更新PHP版本,对于新手来说了解很清楚LAMP环境的搭建是需要掌握的,希望大家阅读完这篇文章能有所收获,下面我们一起来学习一下吧。
在本篇文章里小编给大家分享的是一篇关于php7 没有phpize的解决方法及相关问题总结,有需要的朋友们可以跟着学习下。
本篇文章讲述的是PHP中self与this的5个区别内容,小编提供了此相关知识点给大家阅读查看。本篇文章具有一定的参考价值,希望能让你从中收获满满。
如何实现yii2使用表单上传文件?这篇文章给大家介绍的是YII2框架实现表单中上传单个文件的方法,小编认为是比较实用的,分享给大家参考一下,有这方面学习需求的朋友可以看一下。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008