It’s easy to output complete SQL statements in laravel

Time:2021-1-20

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 – https://github.com/guanguans/…

install

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

Publishing services

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

use

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

<?php
return [
    /*
     * Get sql statement.
     */
    'to_raw_sql' => 'toRawSql',

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

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