Learn Seeding in Laravel

Tram Ho

Introduce

  • Laravel provides an easy way to create sample data in the database using seed classes.
  • The seed classes are located in the database/seeds directory.

  • Seed classes can have any name, but should be set according to some rules, such as PostsTableSeeder .
  • By default, the DatabaseSeeder class is made available, in this class you can call other seeds.

Create Seeders

  • To create a seed, use the command php artisan make:seed [tên_seed] .

  • A seed class only contains the default run .
  • This method will be called when running the command artisan cmd php artisan db:seed .
  • In the run method we will insert data.

Use Factory

  • Depending on the needs, users can insert data manually or use random data.
  • Use the factory model to create multiple records.
  • To create a factory, use the command php artisan make:factory [tên_factory] Example: PostFactory

  • We need to revise the model and start inserting data, below I use faker to create sample data.
  • Some sample faker.
#SyntaxExample results
first$ faker-> randomDigit7
2$ faker-> word‘aut’
3$ faker-> name‘Dr. Zane Stroman ‘
4$ faker-> address‘8888 Cummings Vista Apt. 101, Susanbury, NY 95473 ‘
5$ faker-> phoneNumber‘201-886-0269 x3767’
6$ faker-> company‘Bogan-Treutel’
7$ faker-> text‘Dr. Zane Stroman ‘
9$ faker-> dateTime ($ max = ‘now’, $ timezone = null)DateTime (‘2008-04-25 08:37:17’, ‘UTC’)
ten$ faker-> email[email protected]
11$ faker-> chrome‘Mozilla / 5.0 (Macintosh; PPC Mac OS X 10_6_5) AppleWebKit / 5312 (KHTML, like Gecko) Chrome / 14.0.894.0 Safari / 5312’
twelfth$ faker-> hexcolor‘# fa3cc2’
13$ faker-> fileExtension‘avi’
14$ faker-> imageUrl ($ width = 640, $ height = 480)The image URL is 640 x 480 in size
15$ faker-> uuid‘7e57d004-2b97-0e7a-b45f-5387367791cd’
16$ faker-> ean13‘4006381333931’
17$ faker-> md5‘de99a620c50f2990e87144735cd357e7’
18$ faker-> randomHtml (2,3)Create HTML documents no more than 2 levels deep and no more than 3 elements wide at all levels.

  • After the following definition, in the run function of the PostsTableSeeder class we just need to call factory(tên_lớp, số_lượng)->create() .

Calling Seeders

  • In the DatabaseSeeder class, we can call other seed classes.

  • Note when call the class must be in relative order. Example: Above we run CategoriesTableSeeder first because PostsTableSeeder needs category_id to insert into the category_id column of PostsTableSeeder .

Running Seeders

  • Before running, you should reload the file using composer dump-autoload
  • Using php artisan db:seed to run all files are call in class DatabaseSeeder .
  • Specifies seed a php artisan db:seed --class=UsersTableSeeder class php artisan db:seed --class=UsersTableSeeder .
  • Use php artisan migrate:fresh --seed to delete all tables and then run the tables and seed again.

Conclude

  • Seed allows quick sample creation and reuse.
  • With Faker library allows you to create sample data that is more realistic.
  • Beneficial for application development and test process

References

Share the news now

Source : Viblo