[英]How to use Eloquent ORM's getQueryLog() outside of Laravel?
我一直試圖找出一種方法來記錄我在Zend Framework 1中使用的Eloquent ORM中的SQL查詢。我遇到了以這種方式調用的getQueryLog()方法:
$queries = DB::getQueryLog();
我發現Illuminate \\ Database \\ Connection包含getQueryLog()方法,所以我嘗試執行以下操作:
use Illuminate\Database\Connection as DB;
class IndexController
{
.
.
.
public function indexAction()
{
// do stuff (e.g. fetch/update/create rows)
$questions = Questions::all()
.
.
$queries = DB::getQueryLog();
var_dump($queries); exit;
.
// render view
}
}
但是,我得到以下通知,它返回NULL: Notice: Undefined property: IndexController::$queryLog in /var/www/qasystem/vendor/illuminate/database/Illuminate/Database/Connection.php on line 918 NULL
有人可以建議我如何在Laravel之外使用它嗎? 我在網上搜索過,看不到我需要做的任何事情,盡管我懷疑大多數例子都會在Laravel中使用。 另外,Illuminate \\ Database \\ Connection是正確的類嗎? 謝謝
請改用toSql
方法。 它將返回最終的查詢命令。
請參閱如何讓查詢構建器將其原始SQL查詢作為字符串輸出? 了解更多信息
即使有點老 - 我只是遇到了同樣的問題,並且使用toSql()
並沒有幫助我,因為我有多對多的關系,Eloquent執行了更多的查詢。
根據@patricus的評論,我得到了這樣的工作:
function getTheThing() {
(new Thing())->getConnection()->enableQueryLog();
$thing = Thing::whereUid('something')
->with('AnotherThing')
->first();
$loggedSqls = (new Thing())->getConnection()->getQueryLog();
var_dump($loggedSqls);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.