The query building method toSql in Laravel produces an SQL statement that is not bound to a condition parameter, such as select * fromusersWhereID =? “, so I wrote an extension package laravel-dump-SQL to get the full SQL statement.

The source code

  • Laravel – dump – SQL – github.com/guanguans/l…

The installation

$ composer require guanguans/laravel-dump-sql -v
Copy the code

Publishing service

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

use

After the installation is successful, toRawSql, dumpSql, and ddSql are added to the query builder

/ / access to SQL
User::where('id'.1)->toRawSql();
DB::table('user')->where('id'.1)->toRawSql();

/ / print the SQL
User::where('id'.1)->dumpSql();
DB::table('user')->where('id'.1)->dumpSql();

// Print the SQL and exit
User::where('id'.1)->ddSql();
DB::table('user')->where('id'.1)->ddSql();
Copy the code

Custom method name

Publishing a Configuration File

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

config/dumpsql.phpThe configuration method name in the file is available


      
return [
    /* * Get sql statement. */
    'to_raw_sql'= >'toRawSql'./* * Print SQL statements. */
    'dump_sql'= >'dumpSql'./* * Print SQL statements and exit. */
    'dd_sql'= >'ddSql',];Copy the code