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

Data filling in laravel framework

灭绝师太
Release: 2021-12-02 12:13:36
Original
1852 people have browsed it

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
    Copy after login
  • Generate UserSeeder class

  • <?php
    namespace Database\Seeders;
    use Illuminate\Database\Seeder;
    use Illuminate\Support\Facades\DB;
    class UserSeeder extends Seeder
    {
        public function run()
        {
            
        }
    }
    Copy after login
  • 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;)],
      ]);
    //相应的类已经在上方导入
    Copy after login
  • Use the Artisan command db:seed --class option to specify a specific seeder class:

  • php artisan db:seed --class=UserSeeder
    Copy after login

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;)],
            ]);
        }
    }
    Copy after login
  • Use the call method to run other seed classes

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

  • php artisan db:seed
    Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template