欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

實(shí)現(xiàn)laravel 插入操作日志到數(shù)據(jù)庫的方法

 更新時(shí)間:2019年10月11日 16:34:32   作者:即墨丹青  
今天小編就為大家分享一篇實(shí)現(xiàn)laravel 插入操作日志到數(shù)據(jù)庫的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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)文章

最新評(píng)論