Seeding Inside Migration File

In Laravel, you can seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you.

Here we are talking about the initial data seeding along with the migration file. So you will get the first data just after migrating the database file.

public function up()
{
    Schema::create('table_name', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->timestamps();
    });

    DB::table('table_name')->insert([
        'name' => 'John Doe',
        'short' => 'Short intro goes here...',
        'description' => 'All the details goes here...',
    ]);
}

In the above example, the up the method creates a table named table_name with columns idname, and timestamps. After creating the table, it inserts three rows of data into the table using the insert method of the DB facade.

Related Posts