What is the procedure for adding a field in laravel 5.2 migration when saving test data?

I am learning and trying to use laravel 5.2, I am confusing how to add a field to a table.

I create a migration file named 2016_01_29_093417_create_thread_table.php, every time I want to add a field, I add code inside the file, for example

$table->string('content');  

and then run the command

php artisan migrate:refresh

a new field will appear in the table, but the test data (for example, the user in the user table will be truncated)

Questions:

1) What is the correct way (best practice) to add a new field to the table? 2) how to save test data in all tables, for example, a user in a user table?

Does anyone know how to do this?

+4
2

.

, , : up() down() . :

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UsersNewField extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function ($table) {
            $table->string('email');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function ($table) {
            $table->dropColumn('email');
        });
    }
}

php artisan migrate .

- , : , , . :

<?php

use Illuminate\Database\Seeder;

class UsersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'name' => 'First User',
            'email' => 'user1@example.com',
            'password' => bcrypt('somepass9019'),
        ]);

        DB::table('users')->insert([
            'name' => 'Second User',
            'email' => 'user2@example.com',
            'password' => bcrypt('somepass2039'),
        ]);

        DB::table('users')->insert([
            'name' => 'Third User',
            'email' => 'user3@example.com',
            'password' => bcrypt('somepass0534'),
        ]);
    }
}

php artisan migrate:refresh --seed reset / .

+2
+1

Source: https://habr.com/ru/post/1628262/


All Articles