<?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');
}
}
<?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);
以上代码,以使用模型进行数据的添加为例进行说明的,模型的其他操作同样可以写入日志,使用构造查询器也可以将对数据的操作存入日志
相关推荐
© 2020 asciim码
人生就是一场修行