Laravel5.1 Learning Note 18 Database 4 data population

Source: Internet
Author: User

    • Introduction
    • Writing seeders
      • Using Model Factories
      • Calling Additional Seeders
    • Running seeders

Introduction

Laravel includes a simple method of the seeding your database with test data using seed classes. All seed classes is stored in database/seeds . Seed classes May has any name of wish, but probably should follow some sensible convention, such as UserTableSeeder , etc. By default, a-class is defined for you DatabaseSeeder . From this class, you may use the call method to run other seed classes, allowing your to control the seeding order.

Writing seeders

To generate a seeder, your may issue the make:seeder Artisan command. All seeders generated by the framework is placed in the database/seeders directory:

php artisan make:seeder UserTableSeeder

A Seeder class only contains one method by default: run . This method was called when the db:seed Artisan Commandis executed. Within run the method, you may insert data to your database however you wish. Thequery Builder to manually insert data or the use eloquent model factories.

As an example, let's modify the DatabaseSeeder class which is included with a default installation of Laravel. Let's add a database insert statement to the run method:

<?phpuse DB;use Illuminate\Database\Seeder;use Illuminate\Database\Eloquent\Model;class DatabaseSeeder extends Seeder{    /**     * Run the database seeds.     *     * @return void     */    public function run()    {        DB::table(‘users‘)->insert([            ‘name‘ => str_random(10),            ‘email‘ => str_random(10).‘@gmail.com‘,            ‘password‘ => bcrypt(‘secret‘),        ]);    }}

Using Model Factories

Of course, manually specifying the attributes for each model seed is cumbersome. Instead, you can use the model factories to conveniently generate large amounts of database records. First, review the Model factory documentation to learn what to define your factories. Once you had defined your factories, you could use the factory helper function to insert records into your database.

For example, let's create users and attach a relationship to each user:

/** * Run the database seeds. * * @return void */public function run(){    factory(‘App\User‘, 50)->create()->each(function($u) {        $u->posts()->save(factory(‘App\Post‘)->make());    });}

Calling Additional Seeders

Within DatabaseSeeder the class, you could use the call method to execute additional seed classes. Using The method allows you to break up call your database seeding into multiple files so, no single seeder class Beco Mes overwhelmingly large. Simply Pass the name of the Seeder class wish to run:

/** * Run the database seeds. * * @return void */public function run(){    Model::unguard();    $this->call(‘UserTableSeeder‘);    $this->call(‘PostsTableSeeder‘);    $this->call(‘CommentsTableSeeder‘);}

Running seeders

Once you had written your seeder classes, you could use the db:seed Artisan command to seed your database. By default, the db:seed command runs DatabaseSeeder the class, which is used to call other seed classes. However, the --class option to specify a specific Seeder class to run individually:

php artisan db:seedphp artisan db:seed --class=UserTableSeeder

You may also seed your database using migrate:refresh the command, which'll also rollback and re-run all of your migrations. This command was useful for completely re-building your database:

php artisan migrate:refresh --seed

Laravel5.1 Learning Note 18 Database 4 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.