akunbeben / eloquent-file
Read SQL file fluently just like a real Database using Eloquent ORM or Query Builder
Requires
- php: ^8.3
- ext-pdo: *
- ext-pdo_sqlite: *
- illuminate/database: ^12.0||^13.0
- illuminate/support: ^12.0||^13.0
Requires (Dev)
- larastan/larastan: ^3.9
- laravel/pao: ^1.0
- laravel/pint: ^1.29
- orchestra/testbench: ^10.0||^11.0
- pestphp/pest: ^4.6
- pestphp/pest-plugin-laravel: ^4.1
- pestphp/pest-plugin-type-coverage: ^4.0
- phpstan/extension-installer: ^1.4
README
Eloquent File
Query a MySQL or MariaDB SQL dump with Laravel's Query Builder and Eloquent, without restoring it to a database server. The dump is loaded into a read-only, in-memory SQLite connection.
Installation
You can install the package via Composer:
composer require akunbeben/eloquent-file
The PHP PDO SQLite extension must be enabled. Laravel discovers the package automatically.
Usage
Open a dump, then query it with the package facade:
use EloquentFile\EloquentFile\Facades\EloquentFile; EloquentFile::open(storage_path('backups/backup.sql')); $activeUsers = EloquentFile::table('users') ->where('status', 'Active') ->orderBy('name') ->get();
The loaded connection is registered as eloquent-file, so it can be used by an Eloquent model:
use Illuminate\Database\Eloquent\Model; class ArchivedUser extends Model { protected $connection = 'eloquent-file'; protected $table = 'users'; } EloquentFile::open(storage_path('backups/backup.sql')); $user = ArchivedUser::find(1);
For dependency injection, use the underlying service directly. A custom connection name may be passed as the second argument:
use EloquentFile\EloquentFile\EloquentFile; $dump = app(EloquentFile::class)->open( storage_path('backups/backup.sql'), 'archive', ); $total = $dump->table('invoices')->sum('total');
Write operations throw an Illuminate\Database\QueryException. Opening another dump with the same connection name replaces the previous in-memory connection.
Supported Dumps
The reader supports standard MySQL and MariaDB table dumps containing backtick-quoted CREATE TABLE and INSERT INTO ... VALUES statements, including extended inserts and MySQL string escapes. Input is streamed while the resulting database is held in memory.
MySQL-specific indexes, constraints, generated expressions, procedures, triggers, and views are not recreated. Queries use SQLite semantics, so raw MySQL-only SQL is not portable. Read-only mode prevents application writes; it is not a security sandbox for untrusted PHP code.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Thank you for considering contributing to Eloquent File! Please review our contributing guide to get started.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
Eloquent File is open-sourced software licensed under the MIT license.