php 一次播种多行 Laravel 5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28594076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Seed multiple rows at once laravel 5
提问by Stephan-v
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 array to create some fake data.
我目前正在尝试为我的用户表做种。如果我像这样用 2 行尝试它,它会失败。如果我只使用一个数组而不是 $users 数组中的 2 个数组来创建一些假数据,它就可以正常工作。
What am I doing wrong, what is the proper way to do this?
我做错了什么,这样做的正确方法是什么?
class UserTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
$users = [
['id' => 1, 'name' => 'Stephan de Vries', 'username' => 'stephan', 'email' => '[email protected]', 'password' => bcrypt('carrotz124')],
['id' => 2, 'name' => 'John doe', 'username' => 'johnny', 'email' => '[email protected]', 'password' => bcrypt('carrotz1243')],
];
User::create($users);
}
}
回答by lukasgeiter
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()
and insert
:
DB::table('users')->insert($users);
Actually you can also call insert()
on the model (the resulting query is the same)
其实你也可以调用insert()
模型(结果查询是一样的)
User::insert($users);
Noteif you choose the insert
method you loose special Eloquent functionality such as timestamps and model events.
请注意,如果您选择了这种insert
方法,则会失去 Eloquent 的特殊功能,例如时间戳和模型事件。
回答by lewis4u
This works, even for Laravel 5.3
这有效,即使对于 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' => '[email protected]',
'password' => bcrypt('password'),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
],
[
'name' => 'Agency',
'email' => '[email protected]',
'password' => bcrypt('password'),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
],
[
'name' => 'End',
'email' => '[email protected]',
'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 "; }
}
}
回答by Simple Test
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'),
]);
}
}
回答by Rikesh Shrestha
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' => '[email protected]', 'password' => bcrypt('carrotz124')],
['id' => 2, 'name' => 'John doe', 'username' => 'johnny', 'email' => '[email protected]', 'password' => bcrypt('carrotz1243')],
];
User::insert($users);
}
}
回答by DotComLab ZA
If anyone is struggling with this, I have been using the following since Laravel 5and can confirm is still working in Laravel 7+...
如果有人为此而苦苦挣扎,我自Laravel 5以来一直在使用以下内容,并且可以确认仍在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' => '[email protected]',
'password' => bcrypt('carrotz124'
),
1 =>
array (
'id' => 2,
'name' => 'John doe',
'username' => 'johnny',
'email' => '[email protected]',
'password' => bcrypt('carrotz1243'
),
));
}}