An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Display formatted SQL queries in your Laravel views

[![GitHub license](https://img.shields.io/github/license/sebdesign/blade-sql-formatter)](https://github.com/sebdesign/blade-sql-formatter/blob/main/LICENSE.md)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/sebdesign/blade-sql-formatter.svg)](https://packagist.org/packages/sebdesign/blade-sql-formatter)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/sebdesign/blade-sql-formatter/run-tests.yml?branch=main&label=tests)](https://github.com/sebdesign/blade-sql-formatter/actions/workflows/run-tests.yml?query=branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/sebdesign/blade-sql-formatter/fix-php-code-style-issues.yml?branch=main&label=code%20style)](https://github.com/sebdesign/blade-sql-formatter/actions/workflows/fix-php-code-style-issues.yml?query=branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/sebdesign/blade-sql-formatter.svg)](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.