https://github.com/sebdesign/blade-sql-formatter
Display formatted SQL queries in your Laravel views
https://github.com/sebdesign/blade-sql-formatter
blade doctrine laravel query sql
Last synced: 4 months ago
JSON representation
Display formatted SQL queries in your Laravel views
- Host: GitHub
- URL: https://github.com/sebdesign/blade-sql-formatter
- Owner: sebdesign
- License: mit
- Created: 2021-11-24T20:52:50.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-01T11:42:01.000Z (about 1 year ago)
- Last Synced: 2025-10-30T10:27:45.570Z (5 months ago)
- Topics: blade, doctrine, laravel, query, sql
- Language: PHP
- Homepage:
- Size: 56.6 KB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Display formatted SQL queries in your Laravel views
[](https://github.com/sebdesign/blade-sql-formatter/blob/main/LICENSE.md)
[](https://packagist.org/packages/sebdesign/blade-sql-formatter)
[](https://github.com/sebdesign/blade-sql-formatter/actions/workflows/run-tests.yml?query=branch%3Amain)
[](https://github.com/sebdesign/blade-sql-formatter/actions/workflows/fix-php-code-style-issues.yml?query=branch%3Amain)
[](https://packagist.org/packages/sebdesign/blade-sql-formatter)
A small Laravel package for formatting SQL statements and files inside your Blade templates.
This package uses [`doctrine/sql-formatter`](https://github.com/doctrine/sql-formatter) to indent and add line breaks in addition to syntax highlighting.
## Installation
> **Requires [PHP 8.1+](https://php.net/releases) and [Laravel 10+](https://laravel.com/docs/10.x/releases)**
You can install the package via composer:
```bash
composer require sebdesign/blade-sql-formatter
```
You can publish the config file with:
```bash
php artisan vendor:publish --tag="blade-sql-formatter-config"
```
This is the contents of the published config file:
```php
HtmlHighlighter::class,
'html_attributes' => [
HtmlHighlighter::HIGHLIGHT_QUOTE => 'style="color: blue;"',
HtmlHighlighter::HIGHLIGHT_BACKTICK_QUOTE => 'style="color: purple;"',
HtmlHighlighter::HIGHLIGHT_RESERVED => 'style="font-weight:bold;"',
HtmlHighlighter::HIGHLIGHT_BOUNDARY => '',
HtmlHighlighter::HIGHLIGHT_NUMBER => 'style="color: green;"',
HtmlHighlighter::HIGHLIGHT_WORD => 'style="color: #333;"',
HtmlHighlighter::HIGHLIGHT_ERROR => 'style="background-color: red;"',
HtmlHighlighter::HIGHLIGHT_COMMENT => 'style="color: #aaa;"',
HtmlHighlighter::HIGHLIGHT_VARIABLE => 'style="color: orange;"',
HtmlHighlighter::HIGHLIGHT_PRE => 'style="color: black; background-color: white;"',
],
'use_pre' => true,
'indent_string' => ' ',
];
```
Feel free to customize the style of each token. You can use inline styles or CSS classes, even Tailwind CSS. Check out the demo on [Tailwind Play](https://play.tailwindcss.com/JXXKktftlS).
## Usage
Input:
```
select * from `users` where `id` = 1 limit 1
```
Output:
select
*
from
`users`
where
`id` = 1
limit
1
### View component
```html
select * from `users` where `id` = 1 limit 1
select * from `users` where `id` = 1 limit 1
select * from `users` where `id` = 1 limit 1
select * from `users` where `id` = 1 limit 1
```
### HTML Entity Encoding
If you use Blade's `{{ }}` echo statements inside the `` component, they will be sent through `htmlspecialchars` automatically.
If your SQL statement contains single `'` or double `"` quotes, they will be double-encoded.
For example:
```html
@php($sql = "select * from `users` where `email` = 'info@example.com'")
{{ $sql }}
```
Will output:
select
*
from
`users`
where
`email` = & #039;info@example.com'
In order to address this, you can use raw echo statements `{!! !!}` on your own responsibility.
> Learn more about [displaying unescaped data](https://laravel.com/docs/8.x/blade#displaying-unescaped-data).
For example:
```html
@php($sql = "select * from `users` where `email` = 'info@example.com'")
{!! $sql !!}
```
Will output:
select
*
from
`users`
where
`email` = 'info@example.com'
### Blade directive
```html
@sql('select * from `users` where `id` = 1 limit 1')
@sql
select * from `users` where `id` = 1 limit 1
@endsql
```
### Rendering and including views
You can render and include SQL files like Blade files inside your controllers and your views. The SQL files will be compiled as formatted and highlighted HTML files, and will be cached in the compiled view path, e.g.: `storage/framework/views`.
If you don't want to store you SQL files in the `resources/views` directory, you can load them from another location, by adding the path in the `paths` key of your `config/view.php` file.
The following example will use the `database/queries` directory to find SQL files:
```php
return [
'paths' => [
resource_path('views'),
database_path('queries'),
],
];
```
If you prefer using a namespace to separate your Blade files from your SQL files, you can add one in the `boot` method of a service provider.
The following example will use the `database/queries` directory to find SQL files with the `sql::` namespace:
> In this case you don't need to add the path to `config/view.php`.
```php
public function boot()
{
$this->loadViewsFrom(database_path('queries'), 'sql');
}
```
Render `database/queries/users/select-first-user.sql` from a controller.
```php
public function show()
{
return view('sql::users.select-first-user');
}
```
Include `database/queries/users/select-first-user.sql` within a Blade view.
```php
@include('sql::users.select-first-user')
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](.github/SECURITY.md) on how to report security vulnerabilities.
## Credits
- [Sébastien Nikolaou](https://github.com/sebdesign)
- [Grégoire Paris](https://github.com/greg0ire)
- [Jeremy Dorn](https://github.com/jdorn)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.