1
votes

I want to add these data after refresh or database migration

public function up()
    {
        Schema::create('locations', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('type');
            $table->integer('pid')->default(0);
            $table->integer('cid')->default(0);
            $table->string('name');
            $table->timestamps();
        });

        DB::table('locations')->insert([
            ['type' => 1, 'name' => 'Istanbul'],
            ['type' => 2, 'name' => 'Istanbul', 'pid' => 1],
            ['type' => 3, 'name' => 'Taksim', 'cid' => 2],
            ['type' => 3, 'name' => 'Beyoglu', 'cid' => 2],
        ]);
    }

but it give me this error:

?[41;1m Illuminate\Database\QueryException ?[49;22m : ?[33mSQLSTATE[42S01]: Ba se table or view already exists: 1050 Table 'locations' already exists (SQL: cre ate table locations (id bigint unsigned not null auto_increment primary key, type int not null, pid int not null default '0', cid int not null default '0', name varchar(255) not null, created_at timestamp null, updated_at ti mestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

How can I solve this?

1
don't put db insert in migration use db seed, to add data in your tables.umefarooq

1 Answers

1
votes

Remove DB:table from your migration and run:

php artisan migrate:refresh

Then

php artisan make:seed LocationTableSeeder

Then add this to run() function:

    DB::table('locations')->insert([
        ['type' => 1, 'name' => 'Istanbul', 'pid' => 0, 'cid' => 0, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s')],
        ['type' => 2, 'name' => 'Istanbul', 'pid' => 1, 'cid' => 0, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s')],
        ['type' => 3, 'name' => 'Taksim', 'cid' => 2, 'pid' => 0, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s')],
        ['type' => 3, 'name' => 'Beyoglu', 'cid' => 2, 'pid' => 0, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s')],
    ]);

Then

php artisan db:seed --class=LocationTableSeeder