It’s easy to output complete SQL statements in laravel


It's easy to output complete SQL statements in laravel

Query construction method in laraveltoSqlThe resulting SQL statement does not bind condition parameters, similar to thisselect * from users where id = ?So I wrote an extension package laravel dump SQL to get the complete SQL statement.

Source code

  • laravel-dump-sql –…


$ composer require guanguans/laravel-dump-sql -v

Publishing services

$ php artisan vendor:publish --provider="Guanguans\LaravelDumpSql\ServiceProvider"


After successful installation, query builder will addtoRawSqldumpSqlddSqlThree methods

//Get SQL
User::where('id', 1)->toRawSql();
DB::table('user')->where('id', 1)->toRawSql();

//Print SQL
User::where('id', 1)->dumpSql();
DB::table('user')->where('id', 1)->dumpSql();

//Print SQL 并退出
User::where('id', 1)->ddSql();
DB::table('user')->where('id', 1)->ddSql();

Custom method name

Publish profile

$ php artisan vendor:publish --tag=laravel-dump-sql

config/dumpsql.phpThe configuration method name in the file can be either

return [
     * Get sql statement.
    'to_raw_sql' => 'toRawSql',

     * Print SQL statements.
    'dump_sql' => 'dumpSql',

     * Print SQL statements and exit.
    'dd_sql' => 'ddSql',