【发布时间】:2014-06-20 01:57:20
【问题描述】:
在 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);
}
}
【问题讨论】:
标签: php mysql laravel-4 migration