實(shí)現(xiàn)laravel 插入操作日志到數(shù)據(jù)庫的方法
1 . 創(chuàng)建一個(gè)中間件
執(zhí)行: php artisan make:middleware OperationLog
2 . 在中間件中編寫一個(gè)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(); //操作的內(nèi)容 $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 .創(chuàng)建一個(gè)OperationLog模型(這里我放在Models文件夾下了)
執(zhí)行 : 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 . 將中間件注冊(cè)到Kernel.php 文件
/** * The application's global HTTP middleware stack. * * 這些中間件是在對(duì)應(yīng)用程序的每次請(qǐng)求中運(yùn)行的 * * @var array */ protected $middleware = [ ......., ......., ......., \App\Http\Middleware\OperationLog::class, ];
大功告成…
以上這篇實(shí)現(xiàn)laravel 插入操作日志到數(shù)據(jù)庫的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射
下面小編就為大家?guī)硪黄仙U凱HP面向?qū)ο笾畼?biāo)識(shí)映射。小編覺得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06微信公眾號(hào)支付之坑:調(diào)用支付jsapi缺少參數(shù) timeStamp等錯(cuò)誤解決方法
這段時(shí)間一直比較忙,一忙起來真感覺自己就只是一臺(tái)掙錢的機(jī)器了(說的好像能掙到多少錢似的,呵呵);這會(huì)難得有點(diǎn)兒空閑時(shí)間,想把前段時(shí)間開發(fā)微信公眾號(hào)支付遇到問題及解決方法跟大家分享下,這些“暗坑”能不掉就不掉吧,要不然關(guān)鍵時(shí)刻出問題,真是讓人急的焦頭爛額2016-01-01php bcdiv和bcmul 函數(shù)的怪異現(xiàn)象
這篇文章主要介紹了php bcdiv和bcmul 函數(shù)的怪異現(xiàn)象,本文通過實(shí)例代碼講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04PHP 使用 Imagick 裁切/生成縮略圖/添加水印自動(dòng)檢測和處理 GIF
這篇文章主要介紹了PHP 使用 Imagick 裁切/生成縮略圖/添加水印自動(dòng)檢測和處理 GIF的相關(guān)資料,需要的朋友可以參考下2016-02-02PHP實(shí)現(xiàn)網(wǎng)站訪問量計(jì)數(shù)器
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)網(wǎng)站訪問量計(jì)數(shù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10thinkphp3.2中Lite文件替換框架入口文件或應(yīng)用入口文件的方法
這篇文章主要介紹了thinkphp3.2中Lite文件替換框架入口文件或應(yīng)用入口文件的方法,涉及ThinkPHP相關(guān)配置技巧,需要的朋友可以參考下2015-05-05