Seed multiple rows at once laravel 5

前端 未结 6 1099
情深已故
情深已故 2021-02-01 12:44

I\'m currently trying to seed my users table. If I try it like this with 2 rows, it fails. It works fine if I just use a single array instead of the 2 arrays inside the $users a

6条回答
  •  醉梦人生
    2021-02-01 13:02

    If anyone is struggling with this, I have been using the following since Laravel 5 and can confirm is still working in Laravel 7+...

    class UserTableSeeder extends Seeder {
    
    public function run()
    {
        \DB::table('users')->delete();
    
        \DB::table('users')->insert(array (
            0 => 
              array (
                     'id' => 1,
                     'name' => 'Stephan de Vries',
                     'username' => 'stephan',
                     'email' => 'stephan-v@gmail.com',
                     'password' => bcrypt('carrotz124'
             ),
            1 => 
              array (
                     'id' => 2,
                     'name' => 'John doe',
                     'username' => 'johnny',
                     'email' => 'johndoe@gmail.com',
                     'password' => bcrypt('carrotz1243'
             ),
         ));
    
    }}
    

提交回复
热议问题