eloquent 如何在不执行查询的情况下显示查询
How to display query without executing it with eloquent
我想使用 Eloquent 显示查询而不执行它。我想写这样的东西:
\DB::table('my_table')-> updateOrInsert(['a' => 'b'], ['c' => 'd']);
因为是制作动作所以直接运行有点压力。有什么方法可以显示它而不是 运行 吗?
您可以通过以下方式查看原始查询:
$foo = DB::getQueryLog();
var_dump($foo);
它将执行查询。
如果您有查询构建器实例,您可以使用 toSql()
。由于 updateOrInsert()
方法不是 return 构建器实例,因此您无法以这种方式阻止其执行。相反,您可以将查询放在 DB::pretend()
闭包中,它应该 return 它将执行的查询。
\DB::pretend(function() {
\DB::table('my_table')->update(['a' => 'b']);
});
应该return:
[
[
"query" => "update `my_table` set `a` = ?",
"bindings" => [
"b",
],
"time" => 0.01,
],
]
注意:您提供的示例将永远无法运行,因为 insertOrUpdate()
不存在。我相信您正在寻找 updateOrInsert()
我想使用 Eloquent 显示查询而不执行它。我想写这样的东西:
\DB::table('my_table')-> updateOrInsert(['a' => 'b'], ['c' => 'd']);
因为是制作动作所以直接运行有点压力。有什么方法可以显示它而不是 运行 吗?
您可以通过以下方式查看原始查询:
$foo = DB::getQueryLog();
var_dump($foo);
它将执行查询。
如果您有查询构建器实例,您可以使用 toSql()
。由于 updateOrInsert()
方法不是 return 构建器实例,因此您无法以这种方式阻止其执行。相反,您可以将查询放在 DB::pretend()
闭包中,它应该 return 它将执行的查询。
\DB::pretend(function() {
\DB::table('my_table')->update(['a' => 'b']);
});
应该return:
[
[
"query" => "update `my_table` set `a` = ?",
"bindings" => [
"b",
],
"time" => 0.01,
],
]
注意:您提供的示例将永远无法运行,因为 insertOrUpdate()
不存在。我相信您正在寻找 updateOrInsert()