Laravel framework implements the function of using listeners to record sql statements

不言
Release: 2023-03-30 10:46:02
Original
1850 people have browsed it

This article mainly introduces the Laravel framework to implement the function of using listeners to record sql statements. It analyzes the creation and introduction of the Laravel framework listener and the related operating skills of using the listener to record sql statements in the form of examples. Friends who need it can Refer to the following

The example of this article describes how the Laravel framework implements the function of using listeners to record sql statements. Share it with everyone for your reference, the details are as follows:

Use the listener to record sql statements

1. The event class for monitoring sql statements has been defined, just create the listener class directly :

# 监听sql
make:listener QueryListener --event=Illuminate\Database\Events\QueryExecuted
Copy after login

2. Listener class code

./app/Listeners/ QueryListener.php

<?php
namespace App\Listeners;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Http\Models\OperationLog;
class QueryListener
{
  /**
   * Create the event listener.
   *
   * @return void
   */
  public function __construct()
  {
    //
  }
  /**
   * Handle the event.
   *
   * @param QueryExecuted $event
   * @return void
   */
  public function handle(QueryExecuted $event)
  {
    $sql = str_replace("?", "&#39;%s&#39;", $event->sql);
    $log = vsprintf($sql, $event->bindings);
    # 此处$uid定义是依赖于中间件记录操作日志代码
    $uid = isset($_SERVER[&#39;admin_uid&#39;]) ? $_SERVER[&#39;admin_uid&#39;] : 0;
    if(&#39;select&#39; != substr($log , 0 , 6)){
      if(&#39;insert into `operationLog`&#39; != substr($log , 0 , 26)){
        $OperationLog = new OperationLog();
        $OperationLog->uid = $uid;
        $OperationLog->sql = $log;
        $OperationLog->input = &#39;&#39;;
        $OperationLog->save();
      }
    }
  }
}
Copy after login

3. Introducing the listener

./app/Providers /EventServiceProvider.php

protected $listen = [
    ...
    \Illuminate\Database\Events\QueryExecuted::class => [
      &#39;App\Listeners\QueryListener&#39;
    ],
    ...
  ];
Copy after login

The sql log will be recorded when the operation is performed at this time

Related recommendations:

How to implement fuzzy matching and multi-condition query function in Laravel5

The above is the detailed content of Laravel framework implements the function of using listeners to record sql statements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!