我怎样才能获得 Laravel
查询的 MySQL
查询语句?
转换为中文:
App\User::where('balance','>',0)->where(...)->get();
SELECT * FROM users WHERE `balance`>0 and ...
我怎样才能获得 Laravel
查询的 MySQL
查询语句?
转换为中文:
App\User::where('balance','>',0)->where(...)->get();
SELECT * FROM users WHERE `balance`>0 and ...
使用 laravel 的 toSql()
方法获取将要执行的查询语句
App\User::where('balance','>',0)->where(...)->toSql();
但是,Laravel不会在查询中显示参数,因为它们在查询准备后绑定。要获取绑定的参数,请使用以下代码
$query=App\User::where('balance','>',0)->where(...);
print_r($query->getBindings() );
使用 DB::enableQueryLog()
启用查询日志,并将最后运行的查询输出到屏幕上,你可以使用以下代码:
dd(DB::getQueryLog());
dd(DB::getQueryLog());
select * from `users` where `balance` > ?
- Saeed Vaziry?
放在相应位置,这样你就可以自己输入值,并直接执行。 - RAUSHAN KUMAR$query=App\User::where('balance','>',0)->where(...)->toSql(); print_r( $query->getBindings() );
来获取绑定参数。 - RAUSHAN KUMARfunction getRealQuery($query, $dumpIt = false)
{
$params = array_map(function ($item) {
return "'{$item}'";
}, $query->getBindings());
$result = str_replace_array('\?', $params, $query->toSql());
if ($dumpIt) {
dd($result);
}
return $result;
}
getRealQuery(App\User::where('balance','>',0)->where(...),true)
?
可能是字符串的一部分。 - Paul Spiegel?
,例如这里:->selectRaw("'How are you?' AS message")
。为了获得可靠的解决方案,你将需要一个(类似的)SQL解析器。 - Paul Spiegel想要打印原始的SQL查询语句,请尝试以下方法:
DB::enableQueryLog();
// Your query here
$queries = DB::getQueryLog();
print_r($queries);
要打印单个查询,请使用Laravel的toSql()方法获取要执行的查询,例如
App\User::where('balance','>',0)->where(...)->toSql();
Laravel可以选择在内存中记录当前请求中运行的所有查询。但在某些情况下,例如插入大量行时,这可能会导致应用程序使用过多的内存,因此您应该避免这种情况。
要启用日志记录,您可以使用enableQueryLog方法:
DB::connection()->enableQueryLog();
$queries = DB::getQueryLog();
您可以在这里获取更多详细信息:Laravel 启用查询日志
在 Laravel 中显示所有查询的另一种方法是,无需启用查询日志即可安装 LaravelDebugBar,请从此处下载 Laravel Debug Bar。它是一个软件包,可以让您在开发过程中快速轻松地监控应用程序。
use DB;
public static function getLastSQL()
{
$queries = DB::getQueryLog();
$last_query = end($queries);
// last_query is the SQL with with data binding like
// {
// select ? from sometable where field = ? and field2 = ? ;
// param1,
// param2,
// param3,
// }
// which is hard to read.
$last_query = bindDataToQuery($last_query);
// here, last_query is the last SQL you have executed as normal SQL
// select param1 from sometable where field=param2 and field2 = param3;
return $last_query
}
这里是bindDataToQuery函数,它会将“?”空白处填上真实的参数。
protected static function bindDataToQuery($queryItem){
$query = $queryItem['query'];
$bindings = $queryItem['bindings'];
$arr = explode('?',$query);
$res = '';
foreach($arr as $idx => $ele){
if($idx < count($arr) - 1){
$res = $res.$ele."'".$bindings[$idx]."'";
}
}
$res = $res.$arr[count($arr) -1];
return $res;
}
很奇怪,Laravel竟然没有支持直接获取原始SQL的方法,毕竟现在已经是6版本了...
这里有一个我自己使用的解决方法,可以快速获取带参数的原始SQL,而无需安装任何扩展...
只需要故意让原始SQL出错即可
比如改变 SQL 语句
DB::table('user')
为了
DB::table('user1')
如果完全不存在“user1”表格!
然后再运行一次。
laravel会报告异常。
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'user1' doesn't exist (SQL: ...)
现在你可以看到带有参数的原始SQL语句就在字符串“(SQL:”后面。
将错误的表名改回正确的表名,然后就完成了!
DB::enableQueryLog();
App\User::where('balance','>',0)->where(...)->get();
print_r(DB::getQueryLog());
public function boot()
{
if (App::isLocal()) {
DB::listen(
function ($sql) {
// $sql is an object with the properties:
// sql: The query
// bindings: the sql query variables
// time: The execution time for the query
// connectionName: The name of the connection
// To save the executed queries to file:
// Process the sql and the bindings:
foreach ($sql->bindings as $i => $binding) {
if ($binding instanceof \DateTime) {
$sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
} else {
if (is_string($binding)) {
$sql->bindings[$i] = "'$binding'";
}
}
}
// Insert bindings into query
$query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);
$query = vsprintf($query, $sql->bindings);
// Save the query to file
/*$logFile = fopen(
storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'),
'a+'
);*/
Log::notice("[USER] $query");
}
);
}
}
不要使用打印语句或"dd
"干扰应用程序,当我想查看生成的SQL时,我会采取以下措施:
DB::listen(function ($query) {
Log::info($query->sql, $query->bindings);
});
// (DB and Log are the facades in Illuminate\Support\Facades namespace)
storage/logs/laravel.log
)。跟踪该文件的写入是一个有用的命令。tail -n0 -f storage/logs/laravel.log
试试这个:
$results = App\User::where('balance','>',0)->where(...)->toSql();
dd($results);