LaravelPackages.net
Acme Inc.
Toggle sidebar
zainularfeen/laravel-seeder-generator

Laravel package to generate seeders from an existing database.

12
1
About zainularfeen/laravel-seeder-generator

zainularfeen/laravel-seeder-generator is a Laravel package for laravel package to generate seeders from an existing database.. It currently has 1 GitHub stars and 12 downloads on Packagist. Install it with composer require zainularfeen/laravel-seeder-generator. Discover more Laravel packages by zainularfeen or browse all Laravel packages to compare alternatives.

Last updated

Laravel Seeder Generator

Latest Stable Version Total Downloads License

Laravel Seeder Generator is a powerful package that allows you to generate seeders from existing database tables with hardcoded data. This is extremely useful for creating backups or replicating database states across different environments.


πŸš€ Features

βœ… Automatically generates seeders from database tables
βœ… Handles large datasets with chunking
βœ… Option to overwrite existing seeders
βœ… Efficient memory handling using Laravel's cursor
βœ… Easy installation and simple usage
βœ… Supports Laravel 9+


πŸ“¦ Installation

You can install the package via Composer:

composer require zainularfeen/laravel-seeder-generator --dev

The package will be auto-discovered in Laravel. If not, you can manually register the Service Provider:

// config/app.php
'providers' => [
    Zainularfeen\SeederGenerator\SeederGeneratorServiceProvider::class,
];

⚑ Usage

Generate Seeders for All Tables

Run the following command to generate seeders for all database tables:

php -d memory_limit=-1 artisan generate:seeders

Custom Chunk Size

If your database has a large number of records, you can control how many rows are processed at a time using the --chunk option:

php -d memory_limit=-1 artisan generate:seeders --chunk=500

Force Overwrite Existing Seeders

By default, if a seeder file already exists, it will not be overwritten. Use --force to regenerate all seeders:

php -d memory_limit=-1 artisan generate:seeders --force

Combine Options

You can combine options like this:

php -d memory_limit=-1 artisan generate:seeders --chunk=1000 --force

πŸ› οΈ How It Works

  1. The command fetches all tables from the database.
  2. It skips system tables like migrations and telescope_entries.
  3. It reads all records using Laravel's cursor() to avoid memory issues.
  4. Data is hardcoded into Laravel seeder files inside the database/seeders directory.
  5. You can run php artisan db:seed --class=YourTableSeeder to seed your database.

πŸ“Œ Example Output

After running the command, it generates seeder files inside database/seeders:

<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UsersSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->insert([
            ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]', 'created_at' => now(), 'updated_at' => now()],
            ['id' => 2, 'name' => 'Jane Smith', 'email' => '[email protected]', 'created_at' => now(), 'updated_at' => now()],
        ]);
    }
}

πŸ”„ Running the Generated Seeders

Once the seeders are generated, you can use Laravel's built-in seeding commands:

php artisan db:seed --class=UsersSeeder

To seed all generated seeders at once:

php artisan db:seed

πŸ”§ Troubleshooting

Seeder File Already Exists

❌ "Seeder already exists for users. Use --force to overwrite."

Solution: Use the --force flag:

php artisan generate:seeders --force

Out of Memory Issues

❌ "PHP Fatal error: Allowed memory size exhausted"

Solution: Run the command with unlimited memory:

php -d memory_limit=-1 artisan generate:seeders

🎯 License

This package is open-source software licensed under the MIT license.


🀝 Contributing

Pull requests are welcome! If you find a bug or have a feature request, feel free to open an issue.


πŸ† Credits

Developed by Zain Ul Arfeen
GitHub: zainularfeen

Comments