{"id":16675864,"url":"https://github.com/mpyw/laravel-mysql-system-variable-manager","last_synced_at":"2025-04-09T20:32:36.074Z","repository":{"id":45681327,"uuid":"196497309","full_name":"mpyw/laravel-mysql-system-variable-manager","owner":"mpyw","description":"A tiny extension of MySqlConnection that manages session system variables","archived":false,"fork":false,"pushed_at":"2025-03-05T05:55:55.000Z","size":73,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T23:37:07.812Z","etag":null,"topics":["laravel-mysql","mysql","pdo","pdo-mysql","php","sql","variables"],"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/mpyw.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-12T02:48:11.000Z","updated_at":"2025-03-05T05:55:24.000Z","dependencies_parsed_at":"2023-11-21T18:53:13.902Z","dependency_job_id":null,"html_url":"https://github.com/mpyw/laravel-mysql-system-variable-manager","commit_stats":{"total_commits":31,"total_committers":1,"mean_commits":31.0,"dds":0.0,"last_synced_commit":"a7ebb26ddc6704810499f1d2d73c56e4505d29e6"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Flaravel-mysql-system-variable-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Flaravel-mysql-system-variable-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Flaravel-mysql-system-variable-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Flaravel-mysql-system-variable-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpyw","download_url":"https://codeload.github.com/mpyw/laravel-mysql-system-variable-manager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248107621,"owners_count":21048967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["laravel-mysql","mysql","pdo","pdo-mysql","php","sql","variables"],"created_at":"2024-10-12T13:08:14.154Z","updated_at":"2025-04-09T20:32:36.041Z","avatar_url":"https://github.com/mpyw.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel MySQL System Variable Manager\u003cbr\u003e[![Build Status](https://github.com/mpyw/laravel-mysql-system-variable-manager/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/mpyw/laravel-mysql-system-variable-manager/actions) [![Coverage Status](https://coveralls.io/repos/github/mpyw/laravel-mysql-system-variable-manager/badge.svg?branch=migrate-ci)](https://coveralls.io/github/mpyw/laravel-mysql-system-variable-manager?branch=migrate-ci)\n\nA tiny extension of `MySqlConnection` that manages **session** system variables\n\n## Requirements\n\n| Package | Version                               | Mandatory |\n|:--------|:--------------------------------------|:---------:|\n| PHP     | \u003ccode\u003e^8.2\u003c/code\u003e                     |     ✅     |\n| Laravel | \u003ccode\u003e^11.0 \u0026#124;\u0026#124; ^12.0\u003c/code\u003e |     ✅     |\n| PHPStan | \u003ccode\u003e\u0026gt;=2.0\u003c/code\u003e                 |           |\n\n\u003e [!NOTE]\n\u003e Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.\n\n## Installing\n\n```bash\ncomposer require mpyw/laravel-mysql-system-variable-manager\n```\n\n## Basic Usage\n\n\u003e [!IMPORTANT]\n\u003e The default implementation is provided by `MySqlConnectionServiceProvider`, however, **package discovery is not available**.\nBe careful that you MUST register it in **`config/app.php`** by yourself.\n\n```php\n\u003c?php\n\nreturn [\n\n    /* ... */\n\n    'providers' =\u003e [\n        /* ... */\n\n        Mpyw\\LaravelMySqlSystemVariableManager\\MySqlConnectionServiceProvider::class,\n\n        /* ... */\n    ],\n\n];\n```\n\n```php\n\u003c?php\n\nuse Illuminate\\Support\\Facades\\DB;\n\n// Assign an auto-recoverable system variable\n// The variable is reassigned on accidental disconnections\nDB::setSystemVariable('long_query_time', 10.0);\n\n// Assign a system variable without auto-recovery\nDB::setSystemVariable('long_query_time', 10.0, false);\n\n// Assign multiple variables\nDB::setSystemVariables(['long_query_time' =\u003e 10.0, 'transaction_isolation' =\u003e 'read-committed']);\n\n// Assign a variable on a different connection\nDB::connection('other_mysql_connection')-\u003esetSystemVariable('long_query_time', 10.0);\n\n// Run callback temporarily assigning a variable\nDB::usingSystemVariable('long_query_time', 10.0, function () {\n    /* ... */\n});\n\n// Run callback temporarily assigning multiple variables\nDB::usingSystemVariables(['long_query_time' =\u003e 10.0, 'transaction_isolation' =\u003e 'read-committed'], function () {\n    /* ... */\n});\n\n// Run callback replacing current value\n// NOTE: You MUST declare closure return types.\nDB::usingSystemVariables(\n    [\n        'long_query_time' =\u003e function (float $currentValue): float {\n             return $currentValue + 5.0;\n        },\n        'sql_mode' =\u003e function (string $currentValue): string {\n             return str_replace('ONLY_FULL_GROUP_BY', '', $currentValue);\n        },\n    ],\n    function () {\n        /* ... */\n    }\n);\n```\n\n\u003e [!CAUTION]\n\u003e Don't use `DB::disconnect()` directly or auto-recovery won't be fired.  \n\u003e Use **`DB::connection()-\u003edisconnect()`** instead.\n\n## Advanced Usage\n\n\u003e [!TIP]\n\u003e You can extend `MySqlConnection` with `ManagesSystemVariables` trait by yourself.\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse App\\Database\\MySqlConnection;\nuse Illuminate\\Database\\Connection;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass DatabaseServiceProvider extends ServiceProvider\n{\n    public function register(): void\n    {\n        Connection::resolverFor('mysql', function (...$parameters) {\n            return new MySqlConnection(...$parameters);\n        });\n    }\n}\n```\n\n```php\n\u003c?php\n\nnamespace App\\Database;\n\nuse Illuminate\\Database\\Connection as BaseMySqlConnection;\nuse Mpyw\\LaravelMySqlSystemVariableManager\\ManagesSystemVariables;\n\nclass MySqlConnection extends BaseMySqlConnection\n{\n    use ManagesSystemVariables;\n    \n    public function withoutForeignKeyChecks(callable $callback, ...$args)\n    {\n        return $this-\u003eusingSystemVariable('foreign_key_checks', false, $callback, ...$args);\n    }\n    \n    public function allowingPartialGroupBy(callable $callback, ...$args)\n    {\n        return $this-\u003eusingSystemVariable('sql_mode', function (string $mode): string {\n            return str_replace('ONLY_FULL_GROUP_BY', '', $mode);\n        }, $callback, ...$args);\n    }\n}\n```\n\n```php\n\u003c?php\n\nuse App\\Post;\nuse Illuminate\\Support\\Facades\\Auth;\nuse Illuminate\\Support\\Facades\\DB;\n\n$post = new Post();\n$post-\u003euser()-\u003eassociate(Auth::user());\n$post-\u003esave();\n\nDB::withoutForeignKeyChecks(function () use ($post) {\n    $post-\u003euser()-\u003eassociate(null);\n    $post-\u003esave();\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpyw%2Flaravel-mysql-system-variable-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpyw%2Flaravel-mysql-system-variable-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpyw%2Flaravel-mysql-system-variable-manager/lists"}