You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
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).
22
-
23
-
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:
@@ -46,10 +46,11 @@ As an example, let's modify the `DatabaseSeeder` class which is included with a
46
46
}
47
47
48
48
<aname="using-model-factories"></a>
49
-
### Using Model Factories
50
-
51
-
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.
For example, let's create 50 users and attach a relationship to each user:
54
55
55
56
/**
@@ -65,9 +66,9 @@ For example, let's create 50 users and attach a relationship to each user:
65
66
}
66
67
67
68
<aname="calling-additional-seeders"></a>
68
-
### Calling Additional Seeders
69
+
### 调用附加填充(Calling Additional Seeders)
69
70
70
-
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:
@@ -84,14 +85,14 @@ Within the `DatabaseSeeder` class, you may use the `call` method to execute addi
84
85
}
85
86
86
87
<aname="running-seeders"></a>
87
-
## Running Seeders
88
+
## 执行填充(Running Seeders)
88
89
89
-
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:
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:
0 commit comments