Home  >  Article  >  PHP Framework  >  Teach you how to easily output complete SQL statements in Laravel

Teach you how to easily output complete SQL statements in Laravel

藏色散人
藏色散人forward
2020-07-17 13:18:084030browse

下面由Laravel教程栏目给大家介绍Laravel中轻松容易的输出完整的SQL语句的方法,希望对需要的朋友有所帮助!


laravel 中自带的查询构建方法 toSql 得到的 sql 语句并未绑定条件参数,类似于这样 select * fromuserswhereid= ?,所以写了个扩展包 laravel-dump-sql ,可以获取完整的 sql 语句。

源码

安装

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

发布服务

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

使用

安装成功后查询构建器会新增 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();

自定义方法名称

发布配置文件

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

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

<?php
return [
    /*
     * Get sql statement.
     */
    &#39;to_raw_sql&#39; => &#39;Your favorite method name&#39;,

    /*
     * Print SQL statements.
     */
    &#39;dump_sql&#39; => &#39;Your favorite method name&#39;,

    /*
     * Print SQL statements and exit.
     */
    &#39;dd_sql&#39; => &#39;Your favorite method name&#39;,
];

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

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete