PHPackages                             schrosis/blade-sql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. schrosis/blade-sql

ActiveLibrary[Database &amp; ORM](/categories/database)

schrosis/blade-sql
==================

Generating SQL with Blade

v1.0.0(5y ago)414[2 issues](https://github.com/schrosis/blade-sql/issues)MITPHPPHP ^7.3|^8.0

Since Mar 15Pushed 5y ago1 watchersCompare

[ Source](https://github.com/schrosis/blade-sql)[ Packagist](https://packagist.org/packages/schrosis/blade-sql)[ RSS](/packages/schrosis-blade-sql/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

blade-sql
=========

[](#blade-sql)

[![License](https://camo.githubusercontent.com/6333c661c8f93d363b9800abd78955881328a68c203e9c04b5af704ed313fcfa/68747470733a2f2f706f7365722e707567782e6f72672f736368726f7369732f626c6164652d73716c2f6c6963656e7365)](//packagist.org/packages/schrosis/blade-sql)[![Latest Stable Version](https://camo.githubusercontent.com/f5dfb752846b0c07f585ed7d6067366617bc4c82100f54e22faffb5491eb3503/68747470733a2f2f706f7365722e707567782e6f72672f736368726f7369732f626c6164652d73716c2f76)](//packagist.org/packages/schrosis/blade-sql)[![Latest Unstable Version](https://camo.githubusercontent.com/d572bae3adc16708dc743167ba519e2c34e793a8ae463245943dbf20824be7c9/68747470733a2f2f706f7365722e707567782e6f72672f736368726f7369732f626c6164652d73716c2f762f756e737461626c65)](//packagist.org/packages/schrosis/blade-sql)[![blade-sql-test](https://github.com/schrosis/blade-sql/actions/workflows/blade-sql.yml/badge.svg)](https://github.com/schrosis/blade-sql/actions/workflows/blade-sql.yml)

---

Introduction
------------

[](#introduction)

blade-sql provides the generation and execution of SQL using Blade, Laravel's template engine

When you want to write a complex SQL with more than 100 rows, isn't it hard to write it in the query builder?
Sometimes you want to write raw SQL for tuning purposes
In such cases, blade-sql is useful!

Requirements
------------

[](#requirements)

- PHP(^7.3|^8.0)
- PDO
- Laravel:(^6.0|^7.0|^8.0)

Installation
------------

[](#installation)

```
composer require schrosis/blade-sql

```

Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider

If you don't use auto-discovery, add the `BladeSQLServiceProvider` to the providers array in `config/app.php`
And if you use facade aliases, add `BladeSQL` to the aliases array

```
Schrosis\BladeSQL\Providers\BladeSQLServiceProvider::class,
```

```
'BladeSQL' => Schrosis\BladeSQL\Facades\BladeSQL::class,
```

### Copy the package config to your local config with the publish command

[](#copy-the-package-config-to-your-local-config-with-the-publish-command)

```
php artisan vendor:publish --provider="Schrosis\BladeSQL\Providers\BladeSQLServiceProvider"

```

Usage
-----

[](#usage)

Create `.blade.php` file for blade-sql in `resources/sql/` dir

```
{{-- For example, resources/sql/users/select.blade.php --}}
SELECT id, name FROM users WHERE id = :id
```

Execute SELECT Query
--------------------

[](#execute-select-query)

Execute a query using the `BladeSQL` facade

```
use Schrosis\BladeSQL\Facades\BladeSQL;

$user = BladeSQL::select('users.select', ['id' => 1])->first();
// (object)[
//     'id' => '1',
//     'name' => 'examplename'
// ]
```

The result of a select query is a `Collection` object of `stdClass`

You can also turn it into a `Collection` object of `Models` or your `Entity`

```
use Schrosis\BladeSQL\Facades\BladeSQL;
use App\Moldes\Todo;
use App\Entities\YourTodoEntity;

$stdClassCollection = BladeSQL::select('todos.select');

// to model collection
$modelCollection = $stdClassCollection
    ->model(Todo::class); // or model(new Todo())

// to entity collection
$entityCollection = $stdClassCollection
    ->entity(YourTodoEntity::class);
```

The `entity` method argument expects a class with a method named `fromArray`
Otherwise, you can use the `Collection::map()`

```
class YourTodoEntity
{
    private $id;
    private $contents;
    private $finished_at;
    private $created_at;

    public static fromArray(array $data)
    {
        // some code
    }
}
```

### Execute INSERT UPDATE DELETE Query

[](#execute-insert-update-delete-query)

You can also use the `BladeSQL` facade to perform an `insert` `update` `delete` query

The way to call these methods is the same as the `select`
The difference is that these methods return the number of rows that have been updated

```
$insertedRowNum = BladeSQL::insert('todos.new', [
    'contents' => 'Implement that function',
]);

$updatedRowNum = BladeSQL::update('todos.done', [
    'id' => 1,
    'finished_at' => \Carbon\Carbon::now(),
]);

$deletedRowNum = BladeSQL::delete('todos.delete', [
    'id' => 1,
]);
```

### Run on a specific connection

[](#run-on-a-specific-connection)

If you want to use a connection other than the default one, call the `setConnection` method

```
// Same string value as the argument of DB::connection()
BladeSQL::setConnection('mysql::write')->update('users.change-password', $queryParams);
// or
// Accept ConnectionInterface
$connection = DB::connection('mysql::write');
BladeSQL::setConnection($connection)->update('users.change-password', $queryParams);
```

Also use it when doing a transaction

Blade Directives and Components
-------------------------------

[](#blade-directives-and-components)

In addition to the basic features of blade, provides directives and components similar to Java's `mybatis`

### @where

[](#where)

Erase the leading `AND` or `OR` and prefix it with `WHERE`

`@where` component is useful when using `@if` to create WHERE clauses

```
@where
AND col_1 = 'value'
AND col_2 = 'value'
@endwhere
```

It will compile like this

```
WHERE col_1 = 'value'
AND col_2 = 'value'

```

If the `$slot` of `@where` is empty, the first `WHERE` is not attached

### @set

[](#set)

Works just like `@where`
Remove the `,` at the end and prefix it with `SET`

`@set` component is useful when using `@if` to create a `SET` clause for an update query.

```
@set
col_1 = 'value',
col_2 = 'value',
@endset
```

It will compile like this

```
SET col_1 = 'value',
col_2 = 'value'

```

### @IN

[](#in)

`@IN` directive Create an `IN` clause from an array

```
@IN(id_list)
```

If `$id_list` is `[1, 2, 3]`, it will be compiled like this

```
IN(?, ?, ?)

```

### @LIKE

[](#like)

`@LIKE` directive escapes values that are bound to the `LIKE` clause

```
@LIKE(name)
```

If `$name` is `'strawberry 100%'`, then it will compile like this

```
LIKE ? ESCAPE '\'

```

And `'strawberry 100%'` will be escaped to `'strawberry 100\%'`

You can also easily add `_` and `%`

```
{{-- if $name is 'strawberry 100%' --}}
@LIKE({name}%) {{-- 'strawberry 100\%%' --}}
@LIKE(_{name}) {{-- '_strawberry 100\%' --}}
@LIKE(%{name}%) {{-- '%strawberry 100\%%' --}}
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1890d ago

Major Versions

v0.0.1-beta → v1.0.02021-03-15

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/54675948?v=4)[schrosis](/maintainers/schrosis)[@schrosis](https://github.com/schrosis)

---

Top Contributors

[![schrosis](https://avatars.githubusercontent.com/u/54675948?v=4)](https://github.com/schrosis "schrosis (82 commits)")

---

Tags

bladelaravelphpquerysqllaravelsqlbladequeryblade-sql

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/schrosis-blade-sql/health.svg)

```
[![Health](https://phpackages.com/badges/schrosis-blade-sql/health.svg)](https://phpackages.com/packages/schrosis-blade-sql)
```

###  Alternatives

[rennokki/laravel-eloquent-query-cache

Adding cache on your Laravel Eloquent queries' results is now a breeze.

1.1k4.0M14](/packages/rennokki-laravel-eloquent-query-cache)[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[supliu/laravel-query-monitor

Laravel Query Monitor

287111.9k](/packages/supliu-laravel-query-monitor)[sarfraznawaz2005/indexer

Laravel package to monitor SELECT queries and offer best possible INDEX fields.

562.7k](/packages/sarfraznawaz2005-indexer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
