从Laravel查询生成器生成原始MySQL查询 [英] Generate The Raw MySQL Query From Laravel Query Builder

查看:79
本文介绍了从Laravel查询生成器生成原始MySQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取laravel查询的mysql查询

转换:

App\User::where('balance','>',0)->where(...)->get();

收件人:

SELECT * FROM users WHERE `balance`>0 and ...

推荐答案

使用laravel的toSql()方法来获取要执行的查询,如

use toSql() method of laravel to get the query to be executed like

App\User::where('balance','>',0)->where(...)->toSql();

但是Laravel不会在查询中显示参数,因为它们是在准备查询后绑定的.要获取绑定参数,请使用此

But Laravel will not show you parameters in your query, because they are bound after preparation of the query. To get the bind parameters, use this

$query=App\User::where('balance','>',0)->where(...);
print_r($query->getBindings() );

启用查询日志为DB::enableQueryLog(),然后将最后一次运行的查询输出到屏幕上,您可以使用它,

enable the query log as DB::enableQueryLog() and then output to the screen the last queries ran you can use this,

dd(DB::getQueryLog());

这篇关于从Laravel查询生成器生成原始MySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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