Insert values during migration run laravel

Insert values during migration run laravel

With migrations in laravel, we can create, modify database tables without using phpmyadmin. In Laravel migrations are stores in project_folder/database/migrations in proper order. Every migration file is associated with the table. We can create migration using php artisan command and the name of migration is combination of time and date when migration command executed (i.e. 2020_06_09_000000_create_users_table). We can create a table with its columns using this file. We can also add columns in any already created table. Migration structure consists of 2 functions: up and down. Up function is used to add, insert, modify table or columns and Down function is used to delete columns from a table or drop a table.

If you want to insert data during migration run in laravel then you can use seeders in Laravel, but I'm going to insert data using the same migration file. I will use DB in my migration file to insert data while run migration. In this migration it is important to create a table first and then insert the data into the created table. I'm using below code to insert data during laravel migration.

First I'm running the command to create migration

php artisan make:migration create_products_table

Modify products migration


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateProductsTable extends Migration
public function up()
    Schema::create(`products`, function (Blueprint $table) {

        array(`name` => `Plan 1`,`price` => 10),
        array(`name` => `Plan 2`,`price` => 50),
        array(`name` => `Plan 3`,`price` => 100),
public function down()

After that Run Command

php artisan migrate


Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

20 Dec 2020

I forgot to use Illuminate\Support\Facades\DB; in migration file. Thanks to u

Add your comment