Laravel Data population

Source: Internet
Author: User

writing shims
PHP Artisan Make:seeder Usertableseeder

Modify the class that comes with the Laravel installation DatabaseSeeder , adding a database insert statement to the run method:

<?phpuse db;use illuminate\database\seeder;use Illuminate\database\eloquent\model;classDatabaseseeder extends seeder{/** * Run Database population * * @return void*/     Publicfunction Run () {db::table ('Users'),Insert (['name'= Str_random (Ten),            'Email'= Str_random (Ten).'@gmail. com',            'Password'= Bcrypt ('Secret'),        ]); }}

Using a model factory

Use the Help function factory to insert a record into the database.

Create 50 users and add an association to each user:

 Public function Run () {    factory ('app\user')->create ()  Each (function ($u) {        $u->posts ()->save (Factory ('app\post') Make ());    });}

Call an extra shim

In the DatabaseSeeder class, use call the method to perform an additional fill class

 Public function Run () {    model::unguard ();    $This->call (usertableseeder::class);    $This->call (poststableseeder::class);    $This->call (commentstableseeder::class);}

Run the Filler

--class=usertableseeder

You can also migrate:refresh populate the database with commands, which can also be rolled back and rerun the migration, which is useful when you need to fully rebuild the database:

PHP Artisan Migrate:refresh--seed

 

Laravel Data population

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.