Laravel 5.2 database data population example

Source: Internet
Author: User

1. Introduction

Laravel contains a simple way to populate the database-use the fill class and test data. All the filling classes are located in the database/seeds directory. The class name of the filling class is completely customized by you, but it is best to follow certain rules, such as readability, such as UserTableSeeder. After Laravel is installed, a DatabaseSeeder class is provided by default. From this class, you can use the call method to run other filling classes, allowing you to control the filling order.

2. Compile the filler

To generate a filler, run the make: seeder command in Artisan. All the pads generated by the framework are located in the database/seeders Directory:

Php artisan make: seeder UserTableSeeder

By default, a filler class contains only one method: run. This method is called when the Artisan command db: seed is run. In the run method, you can insert any data you want to insert into the database. You can use the query builder to manually insert data or use the Eloquent model factory.

For example, let's modify the DatabaseSeeder class that comes with Laravel installation and add a database insert statement to the run method:

<? Php

Use Illuminate \ Database \ Seeder;
Use Illuminate \ Database \ Eloquent \ Model;

Class DatabaseSeeder extends Seeder {
/**
* Run database filling
     *
* @ Return void
*/
Public function run ()
    {
DB: table ('users')-> insert ([
'Name' => str_random (10 ),
'Email '=> str_random (10).' @ gmail.com ',
'Password' => bcrypt ('secret '),
]);
    }
}

Use model factory

Of course, it is cumbersome to manually specify the attributes of each model fill. Instead, we can use the model factory to easily generate a large number of database records. First, view the model factory document to learn how to define the factory. After defining the factory, you can use the help function factory to insert records to the database.

For example, let's create 50 users and add associations to each user:

/**
* Run database filling
 *
* @ Return void
*/
Public function run (){
Factory ('app \ user', 50)-> create ()-> each (function ($ u ){
$ U-> posts ()-> save (factory ('app \ Post')-> make ());
});
}

Call an extra filler

In the DatabaseSeeder class, you can use the call method to execute additional filling classes. The call method allows you to split database filling into multiple files, so that a single filler class will not become huge, you just need to pass the name of the filler class you want to run to the past:

/**
* Run database filling
*
* @ Return void
*/
Public function run (){
Model: unguard ();

$ This-> call (UserTableSeeder: class );
$ This-> call (PostsTableSeeder: class );
$ This-> call (CommentsTableSeeder: class );

Model: reguard ();
}

3. Run the filler

After writing the padding class, you can use the Artisan command db: seed to fill the database. By default, the db: seed command can be used to run the DatabaseSeeder class of other filler classes. However, you can also use the -- class option to specify the independent filler class you want to run:

Php artisan db: seed
Php artisan db: seed -- class = UserTableSeeder

You can also use the migrate: refresh command to fill the database. This command can also be rolled back and re-run the migration, which is useful when you need to completely recreate the database:

Php artisan migrate: refresh -- seed

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.