我正在尝试播种我的用户表。如果我这样尝试2行,它就会失败。如果我只使用单个数组而不是$ users数组中的2个数组来创建一些假数据,它就可以正常工作。
我做错了什么,这样做的正确方法是什么?
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::create($users);
}
}
答案 0 :(得分:46)
如果必须使用该模型,则需要循环:
foreach($users as $user){
User::create($user);
}
否则您可以使用DB::table()
和insert
:
DB::table('users')->insert($users);
实际上你也可以在模型上调用insert()
(结果查询是相同的)
User::insert($users);
注意如果选择insert
方法,则会丢失特殊的Eloquent功能,例如时间戳和模型事件。
答案 1 :(得分:28)
这适用于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 "; }
}
}
答案 2 :(得分:1)
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'),
]);
}
}
答案 3 :(得分:1)
如果有人为此而苦苦挣扎,自 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' => 'stephan-v@gmail.com',
'password' => bcrypt('carrotz124'
),
1 =>
array (
'id' => 2,
'name' => 'John doe',
'username' => 'johnny',
'email' => 'johndoe@gmail.com',
'password' => bcrypt('carrotz1243'
),
));
}}
答案 4 :(得分:0)
您应该使用插入而不是创建。 因此代码如下所示:
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);
}
}
答案 5 :(得分:0)
使用截断
<?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);
}
}