Generate The Raw MySQL Query From Laravel Query Builder

后端 未结 14 1446
醉话见心
醉话见心 2021-02-02 12:04

How can i get mysql query of a laravel query

Convert:

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


        
14条回答
  •  没有蜡笔的小新
    2021-02-02 12:48

    Instead of interfering with the application with print statements or "dds", I do the following when I want to see the generated SQL:

    DB::listen(function ($query) { 
        Log::info($query->sql, $query->bindings);
    });
    
    // (DB and Log are the facades in Illuminate\Support\Facades namespace)
    

    This will output the sql to the Laravel log (located at storage/logs/laravel.log). A useful command for following writes to this file is

    tail -n0 -f storage/logs/laravel.log
    

提交回复
热议问题