博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
laravel调试项目_在Laravel中调试查询
阅读量:2512 次
发布时间:2019-05-11

本文共 5763 字,大约阅读时间需要 19 分钟。

laravel调试项目

Laravel provides a fantastic database query builder and ORM system named Eloquent. It makes running extremely simple and allows you to get records with minimal effort. However, there may be times when you need to build an advanced query and would like to see the actual SQL being generated.

Laravel提供了一个出色的数据库查询生成器和名为Eloquent的ORM系统。 它使运行变得非常简单,并使您可以轻松地获取记录。 但是,有时您可能需要构建高级查询,并且希望查看实际生成SQL。

In this tutorial let's go through some of the ways we can easily get access to this information. Starting with the simplest methods and moving into more powerful packages.

在本教程中,让我们介绍一些可以轻松访问此信息的方法。 从最简单的方法开始,然后过渡到功能更强大的软件包。

Before we jump in let's build an Eloquent query for a fictitious bug report that has been assigned to us:

在我们进入之前,让我们为分配给我们的虚拟错误报告建立一个雄辩的查询:

Issue #22321Problem: Searching users is showing to many resultsSteps to reproduce: I search for "John Doe john@example.org" and I'm getting back hundreds of results.

As we dig into the code, we find this query that seems to be where the problem lies:

当我们深入研究代码时,我们发现此查询似乎是问题所在:

$results = User::where(function($q) use ($request) {    $q->orWhere('email', 'like', '%john@example.org%');    $q->orWhere('first_name', 'like', '%John%');    $q->orWhere('last_name', 'like', '%Doe%');})->get();

Can you already spot the problem? If not, don't worry as we will start debugging this and see what is actually going on.

您已经发现问题了吗? 如果没有,请不要担心,因为我们将开始调试它并查看实际发生的情况。

简单查询调试 (Simple Query Debugging)

The simplest method to see the query generated is by utilizing a ->toSql() method. All that we need to do is replace the closing ->get() with ->toSql(). Then printing out the results with the dd(), die and dump, helper.

查看生成的查询的最简单方法是利用->toSql()方法。 我们需要做的就是用->toSql()代替结束的->get() ->toSql() 。 然后使用dd() ,die和dump,helper打印结果。

Here is the updated query:

这是更新的查询:

$results = User::where(function($q) use ($request) {    $q->orWhere('email', 'like', '%john@example.org%');    $q->orWhere('first_name', 'like', '%John%');    $q->orWhere('last_name', 'like', '%Doe%');})->toSql();dd($results)

Running this in the browser will give us the following generated SQL:

在浏览器中运行此命令将为我们提供以下生成SQL:

select * from `users` where (`email` like ? or `first_name` like ? or `last_name` like ?)

This method is great for quickly seeing the SQL. However, it doesn't include the query bindings, only a ? for where they are to be inserted. Depending on the complexity of the bindings, this may be enough information for you to debug it.

此方法非常适合快速查看SQL。 但是,它不包含查询绑定,仅包含一个? 用于将它们插入的位置。 根据绑定的复杂程度,这可能是足够的信息供您调试。

监听查询事件 (Listening For Query Events)

The second option is to listen for query events on the DB object. To set this up above the query add this little helper:

第二种选择是监听数据库对象上的查询事件。 要在查询上方进行设置,请添加以下小助手:

\DB::listen(function($sql) {    var_dump($sql);});

Now when you load the page in the browser you will get the same output as in the simple query debugging section:

现在,当您在浏览器中加载页面时,您将获得与简单查询调试部分相同的输出:

select * from `users` where (`email` like ? or `first_name` like ? or `last_name` like ?)

DB::listen is also more advanced, and it accepts two additional parameters to give us access to the passed in bindings and the time the query took to run:

DB :: listen也是更高级的,它接受两个附加参数,以使我们可以访问传递的绑定以及查询运行所花费的时间:

\DB::listen(function($sql, $bindings, $time) {    var_dump($sql);    var_dump($bindings);    var_dump($time);});

Running this again will then display the following results:

然后再次运行此命令将显示以下结果:

string 'select * from `users` where (`email` like ? or `first_name` like ? or `last_name` like ?)' (length=89)array (size=3)  0 => string '%john@example.org%' (length=18)  1 => string '%John%' (length=6)  2 => string '%Doe%' (length=5)float 35.63

As you can see, this gives us the query, the bindings, and the time the query took to run. With this, we can match up the bindings to the query.

如您所见,这为我们提供了查询,绑定以及查询运行的时间。 这样,我们就可以匹配查询的绑定。

调试栏包 (Debugbar Package)

Another viable option is to install the package. Once installed you will get a heads up overview of your app that is displayed at the bottom of the browser. It's a very full-featured package and will give you lots of insights into your application.

另一个可行的选择是安装软件包。 安装后,您会在浏览器底部看到应用程序的概述。 这是一个功能非常齐全的软件包,将为您提供有关应用程序的大量见解。

The part we want to focus is the query tab that outputs every query ran to generate the page.

我们要关注的部分是查询选项卡,该选项卡输出运行的每个查询以生成页面。

debugbar-package

This gives us the full query with the bindings inserted into the correct place and a bonus of showing query hints to improve it.

这给了我们完整的查询,并在正确的位置插入了绑定,并显示了查询提示来改进它。

Another advantage of having the query shown instantly like this is you can quickly spot areas that might not be . This can lead to a significate database performance hit, and it's easy to notice as you will see the total number of queries being high.

像这样立即显示查询的另一个好处是,您可以快速发现可能不 。 这可能会导致数据库性能显着下降,并且很容易注意到,因为您将看到查询总数很高。

结论 (Conclusion)

Have you spotted the error with the query now? Instead of using orWhere we needed to be using where. Here is the final code that fixes the bug:

您现在发现查询中的错误了吗? 而不是使用orWhere我们需要使用where 。 这是修复该错误的最终代码:

$results = User::where(function($q) use ($request) {    $q->where('email', 'like', '%john@example.org%');    $q->where('first_name', 'like', '%John%');    $q->where('last_name', 'like', '%Doe%');})->get();

Which gives us the corrected query:

这给了我们正确的查询:

select * from `users` where (`email` like '%john@example.org%' and `first_name` like '%John%' and `last_name` like '%Doe%')

Now the next time you get stuck needing to debug a query you have three tools available in your toolbelt to get the job done. Happy debugging!

现在,下次您需要调试查询时,可以在工具栏中使用三个工具来完成工作。 调试愉快!

If you enjoy Laravel, join my to stay up to date on all the latest Laravel news, tips, and resources.

如果您喜欢Laravel,请加入我的以随时了解Laravel的所有最新新闻,技巧和资源。

翻译自:

laravel调试项目

转载地址:http://piywd.baihongyu.com/

你可能感兴趣的文章
学习进度
查看>>
poj3368 RMQ
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
CentOs7安装rabbitmq
查看>>
(转))iOS App上架AppStore 会遇到的坑
查看>>
解决vmware与主机无法连通的问题
查看>>
做好产品
查看>>
项目管理经验
查看>>
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>