Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Takuaraa's avatar

Create table migration with inserted data

Hello,

I would like to know if it's possible to create a database and also directly add content to that database? I have this to create my database:

class CreateIssueTypesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('issue_types', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('level');
            $table->string('description');
            $table->string('category');
            $table->string('extendedComment');
            $table->timestamps();
        });
    }

What I want is to run "php artisan migrate" and that the table IssueTypes is created with default values like id=1 name=test1...,id=2 name=test2...,... .

If it's possible where and how should I write the code for that?

Thank you.

0 likes
2 replies
Takuaraa's avatar

I just had to add like this:

class CreateIssueTypesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('issue_types', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('level');
            $table->string('description');
            $table->string('category');
            $table->string('extendedComment');
            $table->timestamps();
        });

        // Insert
        DB::table('issue_types')->insert(
            array(
              'id' => 1,
              'name' => 'aaa',
              'level' => 'bbb',
              'description' => 'ccc',
              'category' => 'ddd',
              'extendedComment' => 'eee'
            )
        );

Please or to participate in this conversation.