Laravel5.1 Populating the Database

Source: Internet
Author: User

When we create a good table structure, we usually have to generate some test data to test and deal with this scenario Laravel provides a decent service--seed

Laravel's seeder will be placed in the/database/seeders directory and will provide a databaseseeder in the Databaseseeder Run method you can call other seeder you create

1 Creating a Seeder

Run Artisan Create:

PHP Artisan Make:seeder Articlesseeder
1.1 Populating with builders

There is only one run method in the Seeder we can execute our insertion method in the Run method, which can be used by the builder in run or the Model factory (for the two-point follow-up will be written to OH)

     Public function Run ()    {        DB:: Table (' articles '),insert ([            ' title ' = = str_random),            ' body ' = str_ Random (+),        ]);    }
1.2 Filling with model Factory (common)
     Public function Run ()    {        Factory (\app\user::class, ten),Create ();    }

This creates a total of 10 users.

2 performing shims2.1 Specify Execution

Let's say we execute articlesseeder. This filler can be written like this:

PHP Artisan db:seed--class=articlesseeder
2.2 Default Execution

The default execution is to execute the Databaseseeder this shim:

PHP Artisan Db:seed
2.3 Calling a custom shim

If the Databaseseeder shim is executed, either implement the data population in its run method or call the custom shim:

class extends seeder{    * * *Run the     database seeds.     *     * @return void     */public    function  run ()    {        Model::unguard ();         $this->call (' Articlesseeder ');        Model::reguard ();    }}

Then run:

PHP Artisan Db:seed

Laravel5.1 Populating the Database

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.