laravel调试项目_在Laravel中调试查询

laravel调试项目

Laravel provides a fantastic database query builder and ORM system named Eloquent. It makes running queries 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 #22321
Problem: Searching users is showing to many results
Steps 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 Laravel Debugbar 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.

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

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 eager loaded. 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 weekly Laravel newsletter to stay up to date on all the latest Laravel news, tips, and resources.

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

翻译自: https://scotch.io/tutorials/debugging-queries-in-laravel

laravel调试项目

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值