Seed multiple rows at once laravel 5

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

    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 "; }
    
        }
    }
    

提交回复
热议问题