First, data table filler

A population operation is the operation of writing test data to a data table (add operation) and is a useful feature in the development phase.

1.1 Creation and compilation of filler (seed file)

1.1.1. The default directory of the filler

1.1.2. Create a filler

PHP artisan make: Seeder filler nameTable name + TableSeeder Example: If paper is used as an example, the table name isPaperTableSeeder php artisan make:seeder PaperTableSeederCreated seed file:

1.1.3. [Key point] Write the code of the filler

Note: in the filler file, you can use the DB facade to add data, but it needs to be noted that the DB facade does not need to be introduced by the user, once introduced, an error will be reported, you can directly use. It is recommended to use the DB facade method to write new data. Write data using the DB class:


      

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class PaperTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        \DB::table('paper') -> insert([
            [
                'paper_name'= >'Primary 1 Chinese Test'.'total_score'= >100.'start_time'  => time() + 86400.'duration'= >120.'status'= >1
            ],
            [
                'paper_name'= >'Primary 1 Math Test'.'total_score'= >100.'start_time'  => time() + 86400.'duration'= >120.'status'= >1
            ],
            [
                'paper_name'= >'Physical Education Examination for Grade 1 primary School'.'total_score'= >100.'start_time'  => time() + 86400.'duration'= >120.'status'= >1]]); }}Copy the code

1.2 Execute the filler file

Command:PHP artisan DB :seed --class= Name of the seed to be executed (without.php)Unlike migration files, migration operations are recorded in a separate mapping table. Since there is no record of the execution of a seed file, you need to specify the seed file to be executed during the execution of a seed file.

If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.