Laravel `illuminate/database` 组件的SQL日志打印

本文介绍了在Laravel 5.0及5.5版本中如何进行SQL日志打印。在5.0版本,通过事件监听器和闭包可以捕获并打印SQL语句及执行时间。而在5.5及以上版本,使用`Log`门面的`channel`方法配合自定义`macro`,可以直接获取并输出完整的带绑定参数的SQL语句。注意,低版本的Laravel可能不支持某些方法。
摘要由CSDN通过智能技术生成

Laravel illuminate/database 组件的SQL日志打印

  • 基于illuminate/database 5.0

在低版本的组件里,有一个事件监听字符 illuminate.query,

public function listen(Closure $callback)
{
   
    if (isset($this->events))
    {
   
        $this->events->listen('illuminate.query', $callback);
    }
}

传入一个闭包,然后就可以打印所有的SQL语句,实例:

<?php
/**
 * Notes:
 * File name:DataBaseServe
 * Create by: Jay.Li
 * Created on: 2019/11/11 0011 9:38
 */


namespace App\Serve;

use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Capsule;

class DataBaseServe
{
   
    public $databaseServe = null;

    public function __construct()
    {
   
        $this->databaseServe = new Capsule();
        $this->initDatabase();
    }

    public function initDatabase()
    {
   
        $this->databaseServe->addConnection(require __DIR__ . '/../../config/database.php');
        $this->databaseServe->setEventDispatcher(new Dispatcher(new Container));

        // Make this Capsule instance available globally via static methods... (optional)
        $this->databaseServe->setAsGlobal();

        // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
        $this->databaseServe->bootEloquent
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值