Laravel实现将数据库操作记入日志
16lz
2022-04-12
日志数据表迁移文件
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('logs', function (Blueprint $table) {
$table->id();
$table->string('username',50)->comment('用户名');
$table->string('query')->comment('SQL语句');
$table->string('para')->comment('参数');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('logs');
}
}
Log 模型文件
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Log extends Model
{
use HasFactory;
protected $guarded=[];
}
存入日志记录表
//启用数据操作日志
DB::enableQueryLog();
Book::create(['name'=>'Laravel框架']);
$log['username']="admin";
//(DB::getQueryLog()获取操作日志,是一个二维数组
foreach(DB::getQueryLog() as $operation){
$log['query']=$operation['query'];
$log['para']="";
foreach ($operation['bindings'] as $para){
$log['para'].=$para.",";
}
}
Log::create($log);
更多相关文章
- Kotlin Android(安卓)Studio入门学习研究
- android中常见的错误及解决办法
- android studio运行时报错AVD Nexus_5X_API_P is already runnin
- android 驱动文件权限设置
- Android(安卓)开发笔记1
- Android(安卓)Studio-Gradle项目中添加libs目录
- Android从远程服务器下载文件到本地sd卡中
- Android中的Logcat方法查看内核的日志
- 【JNI】 Android(安卓)JNI入门实例(Windows+Cygwin+Eclipse)