Laravel seed after migrating
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Seeding Data after Migration in Laravel
Introduction: Laravel is an incredible PHP framework that simplifies the development process by providing powerful tools for managing your application's data. Migrations and seeding are two such tools, enabling you to structure your database effectively and quickly generate test data for your app. In this blog post, we will discuss how you can automatically seed tables with test data using migrations in Laravel.
1. Understanding Laravel Migrations:
Migrations are the framework's built-in tool that allows developers to manage their database schema efficiently and consistently across different environments. They are created through the use of the `php artisan make:migration` command and contain a class named `Migration`. Your database tables can be easily structured within this migration file by adding or modifying fields, indexes, and relations.
2. Seeding Data with Artisan Commands:
Artisan commands are another crucial tool in Laravel that help you perform various tasks quickly from the command line. There is a built-in seeding class for your database called `Seeder` which allows you to define data for each table in your application. You can create new seeders with `php artisan make:seeder` and specify their names as needed.
3. Seeding Data after Migrations Automatically:
One of Laravel's most impressive features is its ability to run seeder classes automatically when creating or updating migrations. To achieve this, you need to define a `down` method in your migration class that reverts the changes made by the up method during the migration process. After rolling back these modifications, Laravel will execute the seeding class for each table with appropriate data, ensuring a clean database state every time.
4. Implementation:
To implement automatic seeding after migration, follow these steps:
1. Create a new migration file using `php artisan make:migration` and name it appropriately, e.g., 'add_users_table'.
2. Define your table structure in the up method of the migration, adding fields, indexes, etc. Ensure that the changes made will require a seeding class to populate relevant data.
3. Create a new seeder class, for example, `App\Seeders\UsersTableSeeder`, using the command `php artisan make:seeder UsersTableSeeder`.
4. Implement a method in your seeder class that adds test data and other necessary logic to your database tables. As of Laravel 6.x, you will need to use the `run()` method to perform actions within this method.
5. Update your migration class to include both down and up methods with relevant code to handle the database changes and seeding:
```php
// MigrationFile.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
// Add other fields as needed
});
}
public function down()
{
Schema::dropIfExists('users');
}
```
```php
// UsersTableSeeder.php
use App\Models\User;
class UsersTableSeeder extends Seeder
{
public function run()
{
User::truncate(); // Truncates existing users table data
foreach ($this->getUsersData() as $user) {
User::create($user);
}
}
}
```
6. Register your seeder class in the `AppServiceProvider`'s boot method:
```php
// AppServiceProvider.php
public function boot()
{
$this->loadMigrationsFromDatabasePath();
$this->registerMigrationModels();
if (app()->environment('local')) { // Run only in local environment
$this->loadSeedersFromDirectory(database_path('migrations/seeds'));
}
}
```
7. Run your migration using the `php artisan migrate:fresh` command to create a clean database and run all migrations, including the seeder classes.
Conclusion: By following these steps, you can easily implement an automatic seeding process in Laravel after running migrations. This ensures that your application benefits from consistent data across various environments while streamlining your development workflow. Remember always to use well-written and structured code that adheres to Laravel best practices for maximum efficiency and maintenance.