Laravel 4性能调试–调试SQL查询
来源:互联网 发布:精良分班软件 编辑:程序博客网 时间:2024/06/03 06:01
Laravel 3有个非常好的调试工具。它就是Daylee Rees开发的Anbu。最终被Laravel的创建者Taylor加入到核心中。而Laravel 4, 到现在为止,还未发现这种类型的调试工具。那我们该如调试数据库的SQL查询呢?
下面是几种解决方案:
1、使用Loic Sharma’s Profiler。这是一个PHP5.3的调试器,它基于Laravel3的Anbu。我暂时还未测试,所以请按照文档进行使用吧。
2、使用Profiler by Juy。它基于PHP 5.3,支持Laravel 4。后端基于,sorora/omni,前端基于loic-sharma/profiler,部分特性借鉴papajoker/profiler。
3. Create an event that will listen to illuminate.query
. Open your routes.php
file and add this at the bottom:
Event::listen('illuminate.query',function($sql){ var_dump($sql);});
This will print every query on the screen on the place where it is called so it might not look pretty,especially in loops. Also you might not see the queries on the screen in some cases since they will be “hidden” in the HTML source code (so just view the page source to see them, they will usually be at the top of the page)
4. You can also display the last executed query by placing a code in front of the code that executes a query. For example let say we have this code in our controller:
// Grab all the users$customers= Customer::orderBy('name','asc')->get();
Place the Event::listen code in front of that statement and you will see that query displayed on your screen.
// dump the next executed query and die (dd)Event::listen('illuminate.query',function($sql){ dd($sql);}); // Grab all the users$customers= Customer::orderBy('name','asc')->get();
Keep in mind that this will only display 1 query. If your statement calls more queries they will not get displayed.
5. Put the following code in the App::before
filter to get a dump of your database queries:
DB::listen(function($sql,$bindings,$time){ var_dump($sql); var_dump($bindings);});
6. There is another “trick” that I use sometimes (because having SQL queries displayed all the time is not practical and it messes up the code and design): I intentionally make an error in my SQL statement. For example here I mistyped type
as tyspe
:
$contracts= Contract::where('tyspe','=',0)->orderBy('note','asc')->get();
Since tyspe
doesn’t exist Laravel returns an error,showing me the complete SQL statement:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘tyspe’ in ‘where clause’ (SQL: select * from `contracts` where `tyspe` = ? order by `note` asc) (Bindings: array ( 0 => 0, ))
Let me know if you know of any other solutions in the comments.
- Laravel 4性能调试–调试SQL查询
- laravel 调试
- SQL Server2000 性能调试
- Sql Server性能调试方法
- sql查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME
- SQL SERV ER 查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME
- sql查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME
- sql查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME---解释比较详细
- sql查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME
- 【转】sql查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME
- sql查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME
- 性能调试
- Oracle SQL 性能调试(English)
- 调试 以及打印sql语句查询日志
- laravel 慢sql查询
- Laravel调试利器 — Laravel Debugbar
- 内核性能调试–ftrace
- 内核性能调试–ftrace
- usaco 5.4.1 Canada Tour
- 二叉树的一些简单操作
- const
- Objective-C学习笔记-之文件查找
- android开发的学习路线
- Laravel 4性能调试–调试SQL查询
- 常见的几种表分区
- 强悍的CSS工具组合:Blueprint, Sass, Compass
- (六) 系统数据文件和信息
- bash的环境配置文件
- 终端命令
- 刚接触java,利用一个下午的时间来写了一个简单的日历程序,请多多指教!
- PickerView 的使用方法
- ZOJ Problem Set - 3261 Connections in Galaxy War(并查集)