Home > PHP Framework > Laravel > Easily output complete SQL statements in Laravel

Easily output complete SQL statements in Laravel

藏色散人
Release: 2021-03-24 09:03:09
forward
2880 people have browsed it
laravel 中自带的查询构建方法 toSql 得到的 sql 语句并未绑定条件参数,类似于这样 select * from users where id = ?,所以写了个扩展包 laravel-dump-sql ,可以获取完整的 sql 语句。

源码

  • laravel-dump-sql - https://github.com/guanguans/laravel-dump-sql

安装

$ composer require guanguans/laravel-dump-sql -v
Copy after login

发布服务

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

使用

安装成功后查询构建器会新增 toRawSqldumpSqlddSql 三个方法
// 获取 SQL
User::where('id', 1)->toRawSql();
DB::table('user')->where('id', 1)->toRawSql();

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

// 打印 SQL 并退出
User::where('id', 1)->ddSql();
DB::table('user')->where('id', 1)->ddSql();
Copy after login

自定义方法名称

发布配置文件

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

config/dumpsql.php 文件中配置方法名称既可

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

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

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

推荐:最新的五个Laravel视频教程

The above is the detailed content of Easily output complete SQL statements in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template