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

查看:10
本文介绍了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天全站免登陆