Home  >  Article  >  PHP Framework  >  Data filling in laravel framework

Data filling in laravel framework

灭绝师太
灭绝师太Original
2021-12-02 12:09:091716browse

Data filling in laravel framework

In order to make it easier to fill in data into the database, Laravel specifically defines a filling class that can fill in test data for your database. All filling classes are placed in the database/seeds directory. Next, this article will take you to take a look.

1. Write Seeders and fill in data

  • ##Use Artisan command make:seeder to generate Seeder

  • php artisan make:seeder UserSeeder
  • Generate UserSeeder class

  • <?php
    namespace Database\Seeders;
    use Illuminate\Database\Seeder;
    use Illuminate\Support\Facades\DB;
    class UserSeeder extends Seeder
    {
        public function run()
        {
            
        }
    }
  • Use the query constructor in Insert data into the run method

  • DB::table(&#39;users&#39;)->insert(
      [
      
       [&#39;name&#39; => &#39;321250887&#39;,&#39;email&#39; => &#39;321250887@qq.com&#39;,&#39;password&#39; => bcrypt(&#39;321250887&#39;)],
       [&#39;name&#39; => &#39;321250888&#39;,&#39;email&#39; => &#39;321250888@qq.com&#39;,&#39;password&#39; => bcrypt(&#39;321250888&#39;)],
       [&#39;name&#39; => &#39;321250889&#39;,&#39;email&#39; => &#39;321250889@qq.com&#39;,&#39;password&#39; => bcrypt(&#39;321250889&#39;)],
       [&#39;name&#39; => &#39;3212508810&#39;,&#39;email&#39; => &#39;3212508810@qq.com&#39;,&#39;password&#39; => bcrypt(&#39;3212508810&#39;)],
       [&#39;name&#39; => &#39;3212508811&#39;,&#39;email&#39; => &#39;3212508811@qq.com&#39;,&#39;password&#39; => bcrypt(&#39;3212508811&#39;)],
       [&#39;name&#39; => &#39;3212508812&#39;,&#39;email&#39; => &#39;3212508812@qq.com&#39;,&#39;password&#39; => bcrypt(&#39;3212508812&#39;)],
       [&#39;name&#39; => &#39;3212508813&#39;,&#39;email&#39; => &#39;3212508813@qq.com&#39;,&#39;password&#39; => bcrypt(&#39;3212508813&#39;)],
       [&#39;name&#39; => &#39;3212508814&#39;,&#39;email&#39; => &#39;3212508814@qq.com&#39;,&#39;password&#39; => bcrypt(&#39;3212508814&#39;)],
      ]);
    //相应的类已经在上方导入
  • Use the Artisan command db:seed --class option to specify a specific seeder class:

  • php artisan db:seed --class=UserSeeder

2. Use the default DatabaseSeeder class and fill in the data

  • Use the query constructor to insert data in the run method

  • <?php
    namespace Database\Seeders;
    use Illuminate\Database\Seeder;
    use Illuminate\Support\Facades\DB;
    class DatabaseSeeder extends Seeder
    {
        public function run()
        {
            DB::table(&#39;users&#39;)->insert([
                &#39;name&#39; => &#39;3212508814&#39;,
                &#39;password&#39; => bcrypt(&#39;3212508814&#39;)],
            ]);
        }
    }
  • Use the call method to run other seed classes

  • public function run()
    {
        $this->call([
            UserSeeder::class,
            CategorySeeder::class,
        ]);
    }
  • Use Artisan command db :seed to fill the database

  • php artisan db:seed
Related video tutorial recommendations:

Laravel Video tutorial

The above is the detailed content of Data filling in laravel framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn