mpyw/laravel-pdo-emulation-control
Laravel PDO Emulation Control

Temporarily enable/disable PDO prepared statement emulation.
Requirements
| Package | Version | Mandatory |
|---|---|---|
| PHP | ^8.2 | ✅ |
| Laravel | ^11.0 || ^12.0 || ^13.0 || ^14.0 | ✅ |
| PHPStan | >=2.0 |
[!NOTE] Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.
Installing
composer require mpyw/laravel-pdo-emulation-control
Basic Usage
[!IMPORTANT] The default implementation is provided by
ConnectionServiceProvider, however, package discovery is not available. Be careful that you MUST register it inconfig/app.phpby yourself.
<?php
return [
/* ... */
'providers' => [
/* ... */
Mpyw\LaravelPdoEmulationControl\ConnectionServiceProvider::class,
/* ... */
],
];
<?php
use Illuminate\Support\Facades\DB;
// Temporarily enable PDO prepared statement emulation.
DB::emulated(function () {
// Your code goes here
});
// Temporarily disable PDO prepared statement emulation.
// (Only if you've already configured your connection by options [PDO::ATTR_EMULATE_PREPARES => true])
DB::native(function () {
// Your code goes here
});
[!IMPORTANT] Note that
DB::getPdo()DB::getReadPdo()are not always called even though these methods directly touch thePDOinstances. Connections are lazily resolved as possible as they can.PDO::setAttribute()is called only after thePDOinstance has been created and the socket connection to the database has been really established.
Advanced Usage
[!TIP] You can extend Connection classes with
ControlsEmulationtrait by yourself.
<?php
namespace App\Providers;
use App\Database\MySqlConnection;
use Illuminate\Database\Connection;
use Illuminate\Support\ServiceProvider;
class DatabaseServiceProvider extends ServiceProvider
{
public function register(): void
{
Connection::resolverFor('mysql', function (...$parameters) {
return new MySqlConnection(...$parameters);
});
}
}
<?php
namespace App\Database;
use Illuminate\Database\Connection as BaseMySqlConnection;
use Mpyw\LaravelPdoEmulationControl\ControlsEmulation;
class MySqlConnection extends BaseMySqlConnection
{
use ControlsEmulation;
}
Related Packages
Integration layer allowing use of the Laravel database library in CodeIgniter ap...
laravel-ibmi is a simple DB2 & Toolkit for IBMi service provider for Laravel. It...
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v2.1.2 | ^8.2 | ^11.0 | | MIT | |
| v2.1.0 | ^8.2 | ^11.0 | | MIT | |
| v2.0.5 | ^8.0 | ^9.0 | | MIT | |
| v2.0.4 | ^8.0 | ^9.0 | | MIT | |
| v2.0.3 | ^7.4 | | ^6.0 | | MIT | |
| v2.0.2 | ^7.4 | | ^6.0 | | MIT | |
| v2.0.1 | ^7.4 | | ^6.0 | | MIT | |
| v2.0.0 | ^7.4 | | ^6.0 | | MIT | |
| v1.0.1 | ^7.1 | | ^5.8 | | MIT | |
| v1.0.0 | ^7.1 | ^5.8 | | MIT | |
| v0.1.0 | ^7.1 | ^5.8 | | MIT |