khalidmh/eloquent-sql

A Laravel package for generating SQL insert statements from Eloquent model records.

1.1.1 2024-12-08 16:53 UTC

This package is auto-updated.

Last update: 2025-04-13 15:21:19 UTC


README

EloquentSQL is a Laravel package that generates raw SQL insert queries from Eloquent model records. This package makes it easy to convert your Eloquent models into raw SQL insert statements, which can be useful for debugging, logging, exporting data, or batch insert operations.

🚀 Features

  • Converts Eloquent model data into raw SQL insert statements
  • Ability to exclude / include specific columns
  • Easy to use with any Laravel Eloquent model
  • Ideal for debugging, logging, or data migration

📦 Requirements

  • PHP 7.3 or higher
  • Laravel 8.x or higher

🛠 Installation

You can install the package via Composer:

composer require khalidmh/eloquent-sql

📦 Usage

Generating Insert Query for a Model

use KhalidMh\EloquentSQL\EloquentSQL;
use App\Models\User;

$user = User::find(1);
$sql = EloquentSQL::set($user)->toQuery();

// Output: INSERT INTO `users` (`id`, `name`, `email`, ...) VALUES (1, 'John Doe', 'john@example.com', ...);

Excluding Columns

Specify which columns to be excluded from the insert query

use KhalidMh\EloquentSQL\EloquentSQL;
use App\Models\User;

$user = User::find(1);

$sql = EloquentSQL::set($user)
        ->except(['id', 'created_at', 'updated_at'])
        ->toQuery();


// Output: INSERT INTO `users` (`name`, `email`, ...) VALUES ('John Doe', 'john@example.com', ...);

Including Columns

Specify which columns to be in the insert query

use KhalidMh\EloquentSQL\EloquentSQL;
use App\Models\User;

$user = User::find(1);

$sql = EloquentSQL::set($user)
        ->only(['name', 'email'])
        ->toQuery();


// Output: INSERT INTO `users` (`name`, `email`) VALUES ('John Doe', 'johnjohn@example.com');

Including hidden model attributes

By default Laravel removes hidden attributes from the Model based on the $hidden property, you can use the includeHidden() to add them to the insert query without updating the Model's $hidden property globaly.

use KhalidMh\EloquentSQL\EloquentSQL;
use App\Models\User;

$user = User::find(1);

$sql = EloquentSQL::set($user)
        ->includeHidden()
        ->only(['password', 'remember_token'])
        ->toQuery();

// Output: INSERT INTO `users` (`password`, `remember_token`) VALUES ('password', '8zfuGf0f....');

⚙️ Configuration

No additional configuration is required. EloquentSQL works out-of-the-box with your existing Eloquent models.

📃 License

EloquentSQL is open-source software licensed under the MIT license.

🤝 Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.