校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > 實現laravel 插入操作日志到數據庫的方法

實現laravel 插入操作日志到數據庫的方法

熱門標簽:地圖標注入哪個科目 太原極信防封電銷卡 電銷招聘機器人 南寧crm外呼系統平臺 福泉電話機器人 天津營銷電話機器人加盟代理 事業單位如何百度地圖標注 熱線電話機器人 格陵蘭島地圖標注

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 插入操作日志到數據庫的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • laravel 獲取某個查詢的查詢SQL語句方法
  • laravel實現查詢最后執行的一條sql語句的方法
  • Laravel使用原生sql語句并調用的方法
  • Laravel獲取所有的數據庫表及結構的方法
  • Laravel監聽數據庫訪問,打印SQL的例子

標簽:阿克蘇 通化 郴州 自貢 香港 佳木斯 金華 寶雞

巨人網絡通訊聲明:本文標題《實現laravel 插入操作日志到數據庫的方法》,本文關鍵詞  實現,laravel,插入,操作,日志,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《實現laravel 插入操作日志到數據庫的方法》相關的同類信息!
  • 本頁收集關于實現laravel 插入操作日志到數據庫的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 兰溪市| 临澧县| 密山市| 榆树市| 永新县| 昌黎县| 紫阳县| 霍邱县| 宝兴县| 伊川县| 东乌| 陕西省| 长岛县| 修文县| 富源县| 喀什市| 蓝山县| 盈江县| 开江县| 桑日县| 于都县| 兴化市| 通榆县| 迭部县| 铜川市| 诸暨市| 凤庆县| 白水县| 罗山县| 寻甸| 浙江省| 高邮市| 甘谷县| 密山市| 张家界市| 乐山市| 桂阳县| 广西| 马龙县| 东乡县| 霍林郭勒市|