[Laravel5.1-0.0.6] Seeder automatically fills in test data

Source: Internet
Author: User
[Laravel5.1-0.0.6] Seeder automatically fills in test data

Book: http://www.jianshu.com/users/85c8826ce087/latest_articles

1. what is Seeder?
  • Is a data filler tool;

  • Laravel comes with the database/seeds/DatabaseSeeder. php class;

  • The file is generated under database/seeds/By Command;

2. what is Seeder used?
  • Automatically generate batch test data;

  • After table migration, a series of data can be automatically imported for collaborative development;

3. original Seeder code
 call(UserTableSeeder::class);           Model::reguard();        }}
  • The DatabaseSeeder class contains only the run method, which is called when the data generation command is run;

  • The three database operation methods can be used in the run () method.

  • No return value

4. use Seeder 4.1 Basic use
  • Fill in the content in database/seeds/DatabaseSeeder. php to fill in the data;

  • The content added in the run () method will be filled with the data content in the articles table (using SQL statements ):

    public function run()    {       DB::insert('insert into                    articles(title, content,created_at,updated_at)                            values (?, ?,?,?)',                      ['article-title2','article-content2',                  \Carbon\Carbon::now(),\Carbon\Carbon::now()]                  );    }
  • Run the php artisan db: seed command in the directory. If no prompt is displayed, go to the database table and check that a record is generated;

  • To generate multiple records at the same time, you can add multiple statements and then run the same command.
    Php artisan db: seed;

  • A simple time package: Carbon is recommended;

4.2 generate the filler separately and use
  • Create a new filling class:

php artisan make:seeder ArticleTableSeeder
  • Cut the insert statement previously written in the database/seeds/DatabaseSeeder class into the run () method of the database/seeds/ArticleTableSeeder class;

  • In the DatabaseSeeder class run () method, use the call method to obtain the padding class to run:

public function run(){    $this->call(ArticleTableSeeder::class);}
  • In this way, the data to be filled in each table can be used separately, which is really good;

  • Run the filler:

4.3 run the filler
  • Command:

php artisan db:seed
  • To run a single filler, add -- class = ArticleTableSeeder.

php artisan db:seed --class=ArticleTableSeeder
  • Roll back and run data migration again

php artisan migrate:refresh --seed
4.4 faster batch production data Model factory
  • The model factory is worth further details:
    Http://laravelacademy.org/post/238.html#model-factories

Learning Laravel school notes: http://laravelacademy.org/post/133.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.