Laravel迁移的好处

Jun*_*ooq 7 php mysql migration laravel-4

在laravel中我们使用迁移来创建表,然后使用Seeders来播种表,我没有得到它的好处,因为我们可以通过PHPMYADMIN以正常的方式做到这一点,然后我们需要它,因为我们为它编写了许多行,但我们如何证明这些代码行?

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

class CreateItemsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('owner_id');
            $table->string('name');
            $table->boolean('done');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('items');
    }

}
Run Code Online (Sandbox Code Playgroud)

确实,php artisan命令正在创建迁移,但它们的好处是什么?因为我们有替代方法吗?对于Seeder文件也是如此,因为我们为它编写了很多行

class ItemTableSeeder extends Seeder{

    public function run(){
        DB::table('items')->delete();
    $items= array(
        array(
            'owner_id' => '1',
            'name' => 'Watch The Spectacular Now',
            'done' => True
            ),
        array(
            'owner_id' => '2',
            'name' => 'Watch Avengers',
            'done' => False
            ),
        array(
            'owner_id' => '1',
            'name' => 'Watch The Iron man',
            'done' => False
            ),
        array(
            'owner_id' => '1',
            'name' => 'Divergent',
            'done' => False
            ),
        array(
            'owner_id' => '1',
            'name' => 'Bat Man',
            'done' => False
            ),
        array(
            'owner_id' => '1',
            'name' => 'X-Men Days Of Future Past',
            'done' => False
            )
        );
    DB::table('items')->insert($items);

    }

}
Run Code Online (Sandbox Code Playgroud)

Jar*_*zyk 4

迁移和种子是数据库版本控制。想象一下,有一天您爱上了 PostgreSQL 或 MySQL 以外的任何东西。然后想象一下您想要对多行数据进行一些测试。您会运行 PHPMYADMIN 的等效程序并插入 100、1000 或 10000 行吗?

现在检查一下:

// migration
class CreateCommentsTable extends Migration {

    public function up()
    {
        Schema::create('comments', function(Blueprint $table) {
            $table->increments('id');
            $table->string('body');
            $table->integer('author_id')->unsigned();
            $table->integer('post_id')->unsigned();
            $table->timestamps();
        });
    }

// seeder
class CommentsTableSeeder extends Seeder {

    public function run()
    {
        Eloquent::unguard();

        $faker = Faker::create();

        foreach(range(1, 1000) as $index)
        {
            Comment::create([
                'body' => $faker->sentence(10),
                'author_id' => rand(1,20),
                'post_id' => rand(1,150)
            ]);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Faker是一个很棒的工具,您可以在这里找到: https: //github.com/fzaninotto/Faker

您现在所需要做的就是运行artisan migrate --seed

当然,比自动化种子有更多优势,如果您想更改架构等,您可以通过迁移来更改表。