Skip to content

Commit

Permalink
Merge pull request #45 from wedojava/patch-4
Browse files Browse the repository at this point in the history
Update seeding.md
  • Loading branch information
nauxliu committed Aug 30, 2015
2 parents f36adc7 + b6f62e2 commit 6b813ed
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions seeding.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Database: Seeding
# 数据填充(Database: Seeding)

- [Introduction](#introduction)
- [Writing Seeders](#writing-seeders)
- [Using Model Factories](#using-model-factories)
- [Calling Additional Seeders](#calling-additional-seeders)
- [Running Seeders](#running-seeders)
- [介绍](#introduction)
- [编写填充](#writing-seeders)
- [使用模型工厂](#using-model-factories)
- [调用附加填充](#calling-additional-seeders)
- [执行填充](#running-seeders)

<a name="introduction"></a>
## Introduction
## 介绍(Introduction)

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in `database/seeds`. Seed classes may have any name you wish, but probably should follow some sensible convention, such as `UserTableSeeder`, etc. By default, a `DatabaseSeeder` class is defined for you. From this class, you may use the `call` method to run other seed classes, allowing you to control the seeding order.
Laravel 可以简单的使用 seed 类,填充测试数据到数据库。所有的`seed`类存放在`database/seeds`目录下,可以使用任何你想要的类名称,但是应该遵守某些大小写规范,如 `UserTableSeeder` 等等。默认已经有一个 DatabaseSeeder 类。在这个类里,使用 call 方法执行其他的 seed 类,让你控制填充的顺序。

<a name="writing-seeders"></a>
## Writing Seeders
## 编写填充(Writing Seeders)

To generate a seeder, you may issue the `make:seeder` [Artisan command](/docs/{{version}}/artisan). All seeders generated by the framework will be placed in the `database/seeders` directory:
想要创建一个 seeder,你得能在 Laravel 根目录下运行[Artisan 命令](/docs/{{version}}/artisan)才可以。所有的 seeder 被框架成功创建后会放在 `database/seeders` 目录下:

php artisan make:seeder UserTableSeeder

A seeder class only contains one method by default: `run`. This method is called when the `db:seed` [Artisan command](/docs/{{version}}/artisan) is executed. Within the `run` method, you may insert data into your database however you wish. You may use the [query builder](/docs/{{version}}/queries) to manually insert data or you may use [Eloquent model factories](/docs/{{version}}/testing#model-factories).

As an example, let's modify the `DatabaseSeeder` class which is included with a default installation of Laravel. Let's add a database insert statement to the `run` method:
默认的,一个 seeder 类只能包含一个方法:`run`,当你成功运行`db:seed` [Artisan 命令](/docs/{{version}}/artisan)时,这个方法就会被调用了。在 `run` 方法里,你可以插入任何你像要插入的数据到你的数据库
你可以用[query builder](/docs/{{version}}/queries)[Eloquent model factories](/docs/{{version}}/testing#model-factories)手动的插入数据。
例如,我们修改一个安装 Laravel 时默认会在 `database/seeds/` 目录下包含的`DatabaseSeeder`类,我们来为 `run` 方法添加一个数据库插入声明:

<?php

Expand All @@ -46,10 +46,11 @@ As an example, let's modify the `DatabaseSeeder` class which is included with a
}

<a name="using-model-factories"></a>
### Using Model Factories

Of course, manually specifying the attributes for each model seed is cumbersome. Instead, you can use [model factories](/docs/{{version}}/testing#model-factories) to conveniently generate large amounts of database records. First, review the [model factory documentation](/docs/{{version}}/testing#model-factories) to learn how to define your factories. Once you have defined your factories, you may use the `factory` helper function to insert records into your database.
### 使用模型工厂(Using Model Factories)

当然,手动指定每一个模型的 seed 的属性是很累的,更好的办法是用[模型工厂](/docs/{{version}}/testing#model-factories)来生成大量的数据库记录。
首先,回顾下[模型工厂文档](/docs/{{version}}/testing#model-factories)来学会怎么去定义你自己的工厂。一旦你定义好了你的工厂,你可以利用`factory`帮助函数插入数据到你的数据库。
例如,我们创建50个用户并为每个用户添加一个关系:
For example, let's create 50 users and attach a relationship to each user:

/**
Expand All @@ -65,9 +66,9 @@ For example, let's create 50 users and attach a relationship to each user:
}

<a name="calling-additional-seeders"></a>
### Calling Additional Seeders
### 调用附加填充(Calling Additional Seeders)

Within the `DatabaseSeeder` class, you may use the `call` method to execute additional seed classes. Using the `call` method allows you to break up your database seeding into multiple files so that no single seeder class becomes overwhelmingly large. Simply pass the name of the seeder class you wish to run:
`DatabaseSeeder` 类里面,你可以通过`call` 方法来执行附加的 seed 类。`call`方法允许你将数据库填充 seeder 分开写在不同的文件中,这样就不会出现一个 seeder 文件类文件变的特别大,可以简单的通过 seeder 名称,只运行你想运行的 seeder:

/**
* Run the database seeds.
Expand All @@ -84,14 +85,14 @@ Within the `DatabaseSeeder` class, you may use the `call` method to execute addi
}

<a name="running-seeders"></a>
## Running Seeders
## 执行填充(Running Seeders)

Once you have written your seeder classes, you may use the `db:seed` Artisan command to seed your database. By default, the `db:seed` command runs the `DatabaseSeeder` class, which may be used to call other seed classes. However, you may use the `--class` option to specify a specific seeder class to run individually:
当你写好你的填充类(seeder),你可以通过Artisan命令 `db:seed` 来填充数据到数据库。默认情况下,`db:seed` 命令会执行 `DatabaseSeeder`,可以使用它来调用其他 seed 类,不过,也可以使用 `--class` 参数指定要单独执行的类:

php artisan db:seed

php artisan db:seed --class=UserTableSeeder

You may also seed your database using the `migrate:refresh` command, which will also rollback and re-run all of your migrations. This command is useful for completely re-building your database:
你也可以使用 `migrate:refresh` 命令填充数据,它会回滚并且再次执行所有迁移,这个命令往往在你彻底重数据库的时候用:

php artisan migrate:refresh --seed

0 comments on commit 6b813ed

Please sign in to comment.