我想用播种器为多行播种数据库:
public function run() {
DB::table('users')->insert([
[
'name' => 'Guest',
'surname' => 'Guest',
'email' => '[email protected]',
'phone' => '+777',
'password' => bcrypt('password'),
'is_admin' => false,
],
[
'name' => 'Alexander',
'surname' => 'Jones',
'email' => '[email protected]',
'phone' => '+12321321312',
'password' => bcrypt('password'),
'is_admin' => true,
]
]);
但是,当我运行php artisan db:seed
时,它仅在第一行播种。如何为多行播种机? L5.2文档缺少此类信息。请帮助!
通过聊天我们发现实际上是在插入行,但是当通过w / e工具调用DB时,OP所使用的一次仅返回1结果,从而使行出现。
对于使用Sequel Pro(https://sequelpro.com/)的其他用户,我建议查看表视图而不运行查询,因为这可能会引起误解。不确定,但我们认为它在查询末尾增加了限制1。
Try this once..
$mul_rows= [
[ 'name' => 'Guest',
'surname' => 'Guest',
'email' => '[email protected]',
'phone' => '+777',
'password' => bcrypt('password'),
'is_admin' => false,],
[ 'name' => 'Alexander',
'surname' => 'Jones',
'email' => '[email protected]',
'phone' => '+12321321312',
'password' => bcrypt('password'),
'is_admin' => true,]
];
foreach ($mul_rows as $rows) {
//$insert = DB::table('departments')->insert($mul_rows); old
$insert= DB::table('users')->insert($rows);
if($insert){
//success message here
}else{
//Failure message here
}
}