https://github.com/mpyw/laravel-mysql-system-variable-manager
A tiny extension of MySqlConnection that manages session system variables
https://github.com/mpyw/laravel-mysql-system-variable-manager
laravel-mysql mysql pdo pdo-mysql php sql variables
Last synced: 3 months ago
JSON representation
A tiny extension of MySqlConnection that manages session system variables
- Host: GitHub
- URL: https://github.com/mpyw/laravel-mysql-system-variable-manager
- Owner: mpyw
- License: mit
- Created: 2019-07-12T02:48:11.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-05T05:55:55.000Z (4 months ago)
- Last Synced: 2025-04-01T23:37:07.812Z (3 months ago)
- Topics: laravel-mysql, mysql, pdo, pdo-mysql, php, sql, variables
- Language: PHP
- Homepage:
- Size: 71.3 KB
- Stars: 11
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel MySQL System Variable Manager
[](https://github.com/mpyw/laravel-mysql-system-variable-manager/actions) [](https://coveralls.io/github/mpyw/laravel-mysql-system-variable-manager?branch=migrate-ci)A tiny extension of `MySqlConnection` that manages **session** system variables
## Requirements
| Package | Version | Mandatory |
|:--------|:--------------------------------------|:---------:|
| PHP |^8.2
| ✅ |
| Laravel |^11.0 || ^12.0
| ✅ |
| PHPStan |>=2.0
| |> [!NOTE]
> Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.## Installing
```bash
composer require mpyw/laravel-mysql-system-variable-manager
```## Basic Usage
> [!IMPORTANT]
> The default implementation is provided by `MySqlConnectionServiceProvider`, however, **package discovery is not available**.
Be careful that you MUST register it in **`config/app.php`** by yourself.```php
[
/* ... */Mpyw\LaravelMySqlSystemVariableManager\MySqlConnectionServiceProvider::class,
/* ... */
],];
``````php
10.0, 'transaction_isolation' => 'read-committed']);// Assign a variable on a different connection
DB::connection('other_mysql_connection')->setSystemVariable('long_query_time', 10.0);// Run callback temporarily assigning a variable
DB::usingSystemVariable('long_query_time', 10.0, function () {
/* ... */
});// Run callback temporarily assigning multiple variables
DB::usingSystemVariables(['long_query_time' => 10.0, 'transaction_isolation' => 'read-committed'], function () {
/* ... */
});// Run callback replacing current value
// NOTE: You MUST declare closure return types.
DB::usingSystemVariables(
[
'long_query_time' => function (float $currentValue): float {
return $currentValue + 5.0;
},
'sql_mode' => function (string $currentValue): string {
return str_replace('ONLY_FULL_GROUP_BY', '', $currentValue);
},
],
function () {
/* ... */
}
);
```> [!CAUTION]
> Don't use `DB::disconnect()` directly or auto-recovery won't be fired.
> Use **`DB::connection()->disconnect()`** instead.## Advanced Usage
> [!TIP]
> You can extend `MySqlConnection` with `ManagesSystemVariables` trait by yourself.```php
usingSystemVariable('foreign_key_checks', false, $callback, ...$args);
}
public function allowingPartialGroupBy(callable $callback, ...$args)
{
return $this->usingSystemVariable('sql_mode', function (string $mode): string {
return str_replace('ONLY_FULL_GROUP_BY', '', $mode);
}, $callback, ...$args);
}
}
``````php
user()->associate(Auth::user());
$post->save();DB::withoutForeignKeyChecks(function () use ($post) {
$post->user()->associate(null);
$post->save();
});
```