Seed multiple rows at once laravel 5

前端 未结 6 1089
情深已故
情深已故 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:00

    This works, even for Laravel 5.3

    <?php
    
    use Illuminate\Database\Seeder;
    
    class UsersTableSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
    
        public function run()
        {
            // check if table users is empty
            if(DB::table('users')->get()->count() == 0){
    
                DB::table('users')->insert([
    
                    [
                        'name' => 'Administrator',
                        'email' => 'admin@app.com',
                        'password' => bcrypt('password'),
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ],
                    [
                        'name' => 'Agency',
                        'email' => 'agency@app.com',
                        'password' => bcrypt('password'),
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ],
                    [
                        'name' => 'End',
                        'email' => 'endcustomer@app.com',
                        'password' => bcrypt('password'),
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ]
    
                ]);
    
            } else { echo "\e[31mTable is not empty, therefore NOT "; }
    
        }
    }
    
    0 讨论(0)
  • 2021-02-01 13:00

    use truncate

    <?php
    
    use Illuminate\Database\Seeder;
    use App\User;
    
    class UsersTableSeeder extends Seeder
    {
         /**
         * Run the database seeds.
         *
         * @return void
         */
    
      public function run()
        {
    
          User::truncate();
    
            $users =  [
                [
                  'name' => 'Super Admin',
                  'email' => 'superadmin@gmail.com',
                  'password' => '123456',
                ],
                [
                  'name' => 'Account Admin',
                  'email' => 'accountadmin@gmail.com',
                  'password' => '13456',
                ],
                [
                  'name' => 'Project Admin',
                  'email' => 'projectadmin@gmail.com',
                  'password' => '13456',
                ],
                [
                  'name' => 'Client Admin',
                  'email' => 'clientadmin@gmail.com',
                  'password' => '13456',
                ]
              ];
    
              User::create($users);
    
        }
    }
    
    0 讨论(0)
  • 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'
             ),
         ));
    
    }}
    
    0 讨论(0)
  • 2021-02-01 13:08

    If you have to use the model you need a loop:

    foreach($users as $user){
        User::create($user);
    }
    

    Otherwise you can just use DB::table() and insert:

    DB::table('users')->insert($users);
    

    Actually you can also call insert() on the model (the resulting query is the same)

    User::insert($users);
    

    Note if you choose the insert method you loose special Eloquent functionality such as timestamps and model events.

    0 讨论(0)
  • 2021-02-01 13:09

    You should use insert instead of create. So the code will look like this:

    class UserTableSeeder extends Seeder {
    
      public function run()
      {
        DB::table('users')->delete();
    
        $users = [
            ['id' => 1, 'name' => 'Stephan de Vries', 'username' => 'stephan', 'email' => 'stephan-v@gmail.com', 'password' => bcrypt('carrotz124')],
            ['id' => 2, 'name' => 'John doe', 'username' => 'johnny', 'email' => 'johndoe@gmail.com', 'password' => bcrypt('carrotz1243')],
        ];
    
        User::insert($users);
      }
    
    }
    
    0 讨论(0)
  • 2021-02-01 13:19
    public function run()
    {
        //
        for ($i=0; $i < 1000; $i++) { 
             DB::table('seo_contents')->insert([
                'title' => str_random(10),
                'content' => str_random(100),
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s'),
    
            ]);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题