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
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; class UserSeeder extends Seeder { public function run() { } }
DB::table('users')->insert( [ ['name' => '321250887','email' => '321250887@qq.com','password' => bcrypt('321250887')], ['name' => '321250888','email' => '321250888@qq.com','password' => bcrypt('321250888')], ['name' => '321250889','email' => '321250889@qq.com','password' => bcrypt('321250889')], ['name' => '3212508810','email' => '3212508810@qq.com','password' => bcrypt('3212508810')], ['name' => '3212508811','email' => '3212508811@qq.com','password' => bcrypt('3212508811')], ['name' => '3212508812','email' => '3212508812@qq.com','password' => bcrypt('3212508812')], ['name' => '3212508813','email' => '3212508813@qq.com','password' => bcrypt('3212508813')], ['name' => '3212508814','email' => '3212508814@qq.com','password' => bcrypt('3212508814')], ]); //相应的类已经在上方导入
php artisan db:seed --class=UserSeeder
2. Use the default DatabaseSeeder class and fill in the data
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; class DatabaseSeeder extends Seeder { public function run() { DB::table('users')->insert([ 'name' => '3212508814', 'password' => bcrypt('3212508814')], ]); } }
public function run() { $this->call([ UserSeeder::class, CategorySeeder::class, ]); }
php artisan db:seed
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!