How to Call Laravel Seeders from Migrations
In this lesson, we will see how to call Laravel seeders from migrations, so let's assume that we have an admin migration and we want to migrate and seed the table at the same time.
Call Laravel seeder from migration
As we said before I assume that you have an admin seeder already created and we will call it inside the migration here is an example:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
// Call seeder
Artisan::call('db:seed', [
'--class' => 'AdminSeeder',
]);
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('admins');
}
};