Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Sql Formatter Laravel Package

doctrine/sql-formatter

Formats SQL queries into readable, consistently indented output. Helps debug logs, review generated SQL, and improve diffs by standardizing whitespace and keywords. Supports multiple SQL dialect features and runs as a lightweight PHP library.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require doctrine/sql-formatter. Start by using the SqlFormatter class: instantiate it and call format() with a raw SQL string. For debugging, simply wrap query output in logs or exceptions:

use Doctrine\SqlFormatter\SqlFormatter;

$formatter = new SqlFormatter();
$formatted = $formatter->format('SELECT * FROM users WHERE id = 1');
echo $formatted;
//多行缩进、关键词高亮的美化输出

First use case: injecting formatted SQL into Laravel’s query log (DB::enableQueryLog()) or exception handling to make slow/failing queries instantly readable.

Implementation Patterns

  • Logging integrations: Use Laravel’s logging middleware or custom channels to automatically format queries before writing to laravel.log or external services (e.g., via a logger decorator).
  • Testing: Assert against formatted SQL in integration tests (e.g., expect($sql)->toBe($expectedFormatted)); stable output prevents flaky diffs when SQL order/spacing changes but semantics stay identical.
  • Debug Bar / Dusk panels: Drop the formatter into custom SQL panels in Laravel Debugbar or Horizon to show clean query previews.
  • Console commands: In artisan commands that execute raw queries (e.g., migrations, seeds), format output for clarity:
    $sql = SqlFormatter::format($query);
    $this->info($sql);
    
  • Error handling: Append formatted SQL to exception messages for faster diagnosis of database-level failures.

Gotchas and Tips

  • No syntax validation: It only formats—doesn’t validate SQL. Passing malformed SQL (e.g., typos in keywords) may yield nonsensical output or throw exceptions. Always validate queries separately if needed.
  • Whitespace sensitivity: Extra/missing spaces/tabs in the input won’t break formatting, but SQL comments (especially /* */) can干扰 parsing—avoid inline comments near edge cases.
  • Config quirks: While defaults work for most MySQL/Postgres queries, check SqlFormatter::format() options for stripComments, compress, or highlight flags—e.g., disable color codes in CLI loggers using highlight: false.
  • Extension point: Extend SqlFormatter to customize keyword casing, indent size, or line length—but be cautious; minor overrides can break stability guarantees for diff-based tests.
  • Performance: Formatting is fast for typical queries, but avoid wrapping very large query sets (e.g., bulk inserts with 10k+ rows) in production logs—consider sampling instead.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport