Generate The Raw MySQL Query From Laravel Query Builder

后端 未结 14 1174
醉话见心
醉话见心 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 13:03

    Method 1

    To print a single query, use toSql() method of laravel to get the query to be executed like

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

    Method 2

    Laravel can optionally log in memory all queries that have been run for the current request. But in some cases, such as when inserting a large number of rows, this can cause the application to use excess memory, so you should avoid this.

    To enable the log, you may use the enableQueryLog method as

    DB::connection()->enableQueryLog();
    

    To get an array of the executed queries, you may use the getQueryLog method as

    $queries = DB::getQueryLog();
    

    you can get more details here Laravel Enable Query Log

    Method 3

    Another approach to display all queries used in Laravel without enabling the query log install the LaravelDebugBar from here Laravel Debug Bar. It is a package that allows you to quickly and easily keep tabs on your application during development.

    0 讨论(0)
  • 2021-02-02 13:08

    A very simple and shortcut way is below Write the name of column wrong like write 'balancedd' in spite of 'balance' and the query will be displayed on error screen when you execute code with all the parameters and error that column not found.

    0 讨论(0)
提交回复
热议问题