• laravel打印sql语句


    打印sql语句,直接在你执行SQL语句后输出

    方法一:

    $queries = DB::getQueryLog();
    
     $a = end($queries);
    
     $tmp = str_replace('?', '"'.'%s'.'"', $a["query"]);
    
     echo vsprintf($tmp, $a['bindings']);
    
     exit;

    方法二:  可以把下面代码放在查询语句前:

    DB::listen(function($sql, $bindings, $time) {
         foreach ($bindings as $replace){
             $value = is_numeric($replace) ? $replace : "'".$replace."'";
             $sql = preg_replace('/?/', $value, $sql, 1);
         }
         dd($sql);
     })
    

     方法三:

    下载 clockwork 扩展,这个扩展可以在很多框架里调试,比如laravel,lumen,CI等等,很是好用,

    安装完以后,直接在firebug里可以看到执行的语句!

    方法四:

    自己写

    执行

    php artisan make:listener QueryListener

    会生成app/Listeners/QueryListener.php文件

    然后把handler修改成下面这样

    namespace AppListeners;
    
    use IlluminateDatabaseEventsQueryExecuted;
    use IlluminateQueueInteractsWithQueue;
    use IlluminateContractsQueueShouldQueue;
    
    class QueryListener
    {
        /**
         * Create the event listener.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        public function handle(QueryExecuted $event)
        {
            $sql = str_replace("?", "'%s'", $event->sql);
    
            $log = vsprintf($sql, $event->bindings);
    
            Log::info($log);
        }
    
    }

    打开 app/Providers/EventServiceProvider.php ,在 $listen 中添加

        protected $listen = [
            'AppEventsSomeEvent' => [
                'AppListenersEventListener',
            ],
            'IlluminateDatabaseEventsQueryExecuted' => [
                'AppListenersQueryListener'
            ]
        ];

    然后在 自己的storagelog 下看自己的日志吧!

    类似这样

    [2017-01-02 02:50:09] local.INFO: select count(*) as aggregate from `g9zz_posts`  
    [2017-01-02 02:50:09] local.INFO: select * from `g9zz_posts` limit 30 offset 0  
    [2017-01-02 02:50:09] local.INFO: select * from `g9zz_categories` where `g9zz_categories`.`id` in ('1', '6', '5', '3', '4')  
    [2017-01-02 02:50:09] local.INFO: select * from `g9zz_users` where `g9zz_users`.`id` in ('8', '12', '10', '16', '5')  
    [2017-01-02 02:50:09] local.INFO: select * from `g9zz_users` where `g9zz_users`.`id` in ('11', '17', '0')  

    建议把日志换成daily的,不然日志大小会爆炸的哦(config/app.php 里的APP_LOG)

    转载自: http://www.iphpt.com/detail/75/

     

  • 相关阅读:
    idea+maven使用
    MySQL数据库一个字段对应多个值得模糊查询
    什么时候用接口?什么时候用抽象类?
    线程的死锁和死锁解决的实际案例
    idea 实用插件
    linux查看日志报错
    mysql日期模糊查找的方法
    mysql 日期函数
    win10安装sonarqube
    docker中mysql数据库导出部分数据
  • 原文地址:https://www.cnblogs.com/isykw/p/6089551.html
Copyright © 2020-2023  润新知