Generate The Raw MySQL Query From Laravel Query Builder

后端 未结 14 1201
醉话见心
醉话见心 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

    It is so strange that the laravel haven't support any way to get the raw sql easily, it is now version 6 after all...

    Here's a workaround I used by myself to quickly get the raw sql with parameters without installing any extension...

    Just deliberately make your original sql WRONG

    Like change

    DB::table('user')
    

    to

    DB::table('user1')
    

    where the table "user1" does not exist at all!

    Then run it again.

    Sure there will be an exception reported by laravel.

    SQLSTATE[42S02]: Base table or view not found: 1146 Table 'user1' doesn't exist (SQL: ...)
    

    And now you can see the raw sql with parameters is right after the string "(SQL:"

    Change back from the wrong table name to the right one and there you go!

提交回复
热议问题