因为在 Laravel 中我们使用 Migrations 来创建表,然后使用 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');
}
}
的确,迁移是由 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);
}
}
最佳答案
主要的好处是你会在你的开发服务器/站上做,你可能会在开发、迁移、回滚迁移和重新迁移中多次更改架构,一旦你的应用程序 ID 完成,你不必记住你在生产环境中必须做什么,Laravel 会自动为你完成。
使用 PHPMyAdmin,您将不得不在本地和远程服务器上手动创建表格和编辑字段,并且您可能会遗忘一些东西并破坏您的应用程序。而且,如果您有不止一台服务器为您的应用程序提供服务,问题就更大了。
关于php - Laravel 迁移的好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23444124/