Laravel的toSql()方法是否屏蔽ID? (列值由问号代替) [英] Does Laravel's toSql() method mask ids? (column value being replaced by question mark)

查看:227
本文介绍了Laravel的toSql()方法是否屏蔽ID? (列值由问号代替)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试一些在测试套件中执行的SQL查询.使用以下调试代码:

I'm trying to debug some SQL queries that I'm doing in a testing suite. Using the following debugging code:

\Log::debug(User::first()->jobs()->toSql());

打印出的SQL是:

`select * from `jobs` where `jobs`.`deleted_at` is null and `jobs`.`managed_by_id` = ? and `jobs`.`managed_by_id` is not null`

那个问号在那里做什么?我已经测试过查询,并且可以正常工作.是因为我正在选择该first()用户发生这种情况吗?

What is that question mark doing there? I've tested the query, and it works as expected. Is it because i'm selecting that first() user that this is happening?

推荐答案

Laravel使用预处理语句.它们是一种编写SQL语句的方法,而无需将变量直接放入SQL字符串中.您看到的?是信息的占位符或绑定,以后将由PDO替换并自动清除.有关准备好的语句的更多信息,请参见PHP文档. http://php.net/manual /en/pdo.prepared-statements.php

Laravel uses Prepared Statements. They're a way of writing an SQL statement without dropping variables directly into the SQL string. The ? you see are placeholders or bindings for the information which will later be substituted and automatically sanitised by PDO. See the PHP docs for more information on prepared statements http://php.net/manual/en/pdo.prepared-statements.php

要查看将替换为查询字符串的数据,可以按如下所示在查询上调用getBindings()函数.

To view the data that will be substituted into the query string you can call the getBindings() function on the query as below.

$query = User::first()->jobs();

dd($query->toSql(), $query->getBindings());

绑定数组的替换顺序与?在SQL语句中出现的顺序相同.

The array of bindings get substituted in the same order the ? appear in the SQL statement.

这篇关于Laravel的toSql()方法是否屏蔽ID? (列值由问号代替)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆