{"id":36995001,"url":"https://github.com/schrosis/blade-sql","last_synced_at":"2026-01-13T23:47:17.373Z","repository":{"id":62541221,"uuid":"336568681","full_name":"schrosis/blade-sql","owner":"schrosis","description":"Generating SQL with Blade","archived":false,"fork":false,"pushed_at":"2021-03-15T12:20:46.000Z","size":135,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-22T17:45:27.302Z","etag":null,"topics":["blade","laravel","php","query","sql"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/schrosis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-06T15:26:42.000Z","updated_at":"2022-01-25T13:10:23.000Z","dependencies_parsed_at":"2022-11-02T15:45:26.099Z","dependency_job_id":null,"html_url":"https://github.com/schrosis/blade-sql","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/schrosis/blade-sql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schrosis%2Fblade-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schrosis%2Fblade-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schrosis%2Fblade-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schrosis%2Fblade-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/schrosis","download_url":"https://codeload.github.com/schrosis/blade-sql/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schrosis%2Fblade-sql/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28405308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blade","laravel","php","query","sql"],"created_at":"2026-01-13T23:47:17.312Z","updated_at":"2026-01-13T23:47:17.365Z","avatar_url":"https://github.com/schrosis.png","language":"PHP","readme":"# blade-sql\n[![License](https://poser.pugx.org/schrosis/blade-sql/license)](//packagist.org/packages/schrosis/blade-sql)\n[![Latest Stable Version](https://poser.pugx.org/schrosis/blade-sql/v)](//packagist.org/packages/schrosis/blade-sql)\n[![Latest Unstable Version](https://poser.pugx.org/schrosis/blade-sql/v/unstable)](//packagist.org/packages/schrosis/blade-sql)\n[![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)\n\n- - -\n\n## Introduction\n\nblade-sql provides the generation and execution of SQL using Blade, Laravel's template engine\n\nWhen you want to write a complex SQL with more than 100 rows, isn't it hard to write it in the query builder?  \nSometimes you want to write raw SQL for tuning purposes  \nIn such cases, blade-sql is useful!\n\n## Requirements\n\n - PHP(^7.3|^8.0)\n - PDO\n - Laravel:(^6.0|^7.0|^8.0)\n\n## Installation\n\n```\ncomposer require schrosis/blade-sql\n```\n\nLaravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider\n\n\nIf you don't use auto-discovery, add the `BladeSQLServiceProvider` to the providers array in `config/app.php`  \nAnd if you use facade aliases, add `BladeSQL` to the aliases array\n\n```php\nSchrosis\\BladeSQL\\Providers\\BladeSQLServiceProvider::class,\n```\n\n```php\n'BladeSQL' =\u003e Schrosis\\BladeSQL\\Facades\\BladeSQL::class,\n```\n\n### Copy the package config to your local config with the publish command\n\n```\nphp artisan vendor:publish --provider=\"Schrosis\\BladeSQL\\Providers\\BladeSQLServiceProvider\"\n```\n\n## Usage\n\nCreate `.blade.php` file for blade-sql in `resources/sql/` dir\n\n```blade\n{{-- For example, resources/sql/users/select.blade.php --}}\nSELECT id, name FROM users WHERE id = :id\n```\n\n## Execute SELECT Query\nExecute a query using the `BladeSQL` facade\n\n```php\nuse Schrosis\\BladeSQL\\Facades\\BladeSQL;\n\n$user = BladeSQL::select('users.select', ['id' =\u003e 1])-\u003efirst();\n// (object)[\n//     'id' =\u003e '1',\n//     'name' =\u003e 'examplename'\n// ]\n```\n\nThe result of a select query is a `Collection` object of `stdClass`\n\nYou can also turn it into a `Collection` object of `Models` or your `Entity`\n\n```php\nuse Schrosis\\BladeSQL\\Facades\\BladeSQL;\nuse App\\Moldes\\Todo;\nuse App\\Entities\\YourTodoEntity;\n\n$stdClassCollection = BladeSQL::select('todos.select');\n\n// to model collection\n$modelCollection = $stdClassCollection\n    -\u003emodel(Todo::class); // or model(new Todo())\n\n// to entity collection\n$entityCollection = $stdClassCollection\n    -\u003eentity(YourTodoEntity::class);\n```\n\nThe `entity` method argument expects a class with a method named `fromArray`  \nOtherwise, you can use the `Collection::map()`\n\n```php\nclass YourTodoEntity\n{\n    private $id;\n    private $contents;\n    private $finished_at;\n    private $created_at;\n\n    public static fromArray(array $data)\n    {\n        // some code\n    }\n}\n```\n\n### Execute INSERT UPDATE DELETE Query\n\nYou can also use the `BladeSQL` facade to perform an `insert` `update` `delete` query  \n\nThe way to call these methods is the same as the `select`  \nThe difference is that these methods return the number of rows that have been updated\n\n```php\n$insertedRowNum = BladeSQL::insert('todos.new', [\n    'contents' =\u003e 'Implement that function',\n]);\n\n$updatedRowNum = BladeSQL::update('todos.done', [\n    'id' =\u003e 1,\n    'finished_at' =\u003e \\Carbon\\Carbon::now(),\n]);\n\n$deletedRowNum = BladeSQL::delete('todos.delete', [\n    'id' =\u003e 1,\n]);\n```\n\n### Run on a specific connection\n\nIf you want to use a connection other than the default one, call the `setConnection` method\n\n```php\n// Same string value as the argument of DB::connection()\nBladeSQL::setConnection('mysql::write')-\u003eupdate('users.change-password', $queryParams);\n// or\n// Accept ConnectionInterface\n$connection = DB::connection('mysql::write');\nBladeSQL::setConnection($connection)-\u003eupdate('users.change-password', $queryParams);\n```\n\nAlso use it when doing a transaction\n## Blade Directives and Components\n\nIn addition to the basic features of blade, provides directives and components similar to Java's `mybatis`\n\n### @where\n\nErase the leading `AND` or `OR` and prefix it with `WHERE`\n\n`@where` component is useful when using `@if` to create WHERE clauses\n\n```blade\n@where\nAND col_1 = 'value'\nAND col_2 = 'value'\n@endwhere\n```\n\nIt will compile like this\n\n```\nWHERE col_1 = 'value'\nAND col_2 = 'value'\n```\n\nIf the `$slot` of `@where` is empty, the first `WHERE` is not attached\n\n### @set\n\nWorks just like `@where`  \nRemove the `,` at the end and prefix it with `SET`\n\n`@set` component is useful when using `@if` to create a `SET` clause for an update query.\n\n```blade\n@set\ncol_1 = 'value',\ncol_2 = 'value',\n@endset\n```\n\nIt will compile like this\n\n```\nSET col_1 = 'value',\ncol_2 = 'value'\n```\n\n### @IN\n\n`@IN` directive Create an `IN` clause from an array\n\n```blade\n@IN(id_list)\n```\n\nIf `$id_list` is `[1, 2, 3]`, it will be compiled like this\n\n```\nIN(?, ?, ?)\n```\n\n### @LIKE\n\n`@LIKE` directive escapes values that are bound to the `LIKE` clause\n\n```blade\n@LIKE(name)\n```\n\nIf `$name` is `'strawberry 100%'`, then it will compile like this\n\n```\nLIKE ? ESCAPE '\\'\n```\n\nAnd `'strawberry 100%'` will be escaped to `'strawberry 100\\%'`\n\nYou can also easily add `_` and `%`\n\n```blade\n{{-- if $name is 'strawberry 100%' --}}\n@LIKE({name}%) {{-- 'strawberry 100\\%%' --}}\n@LIKE(_{name}) {{-- '_strawberry 100\\%' --}}\n@LIKE(%{name}%) {{-- '%strawberry 100\\%%' --}}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschrosis%2Fblade-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fschrosis%2Fblade-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschrosis%2Fblade-sql/lists"}