我正在使用faker類來幫助播種我的數據庫。該DatabaseSeeder看起來像這樣在laravel中播種時的質量分配錯誤
<?php
class DatabaseSeeder extends Seeder
{
public function run()
{
Eloquent::unguard();
$tables = [
'users',
'posts',
];
foreach ($tables as $table) {
DB::table($table)->truncate();
}
$this->call('UsersTableSeeder');
$this->call('PostsTableSeeder');
}
}
和UsersTableSeeder
<?php
class UsersTableSeeder extends Seeder {
public function run()
{
$faker = Faker\Factory::create();
for($i=0 ; $i<50 ; $i++) {
$user = User::create([
'first_name' => $faker->firstName,
'surname' => $faker->lastName,
'email' => $faker->email,
'username' => $faker->userName,
'bio' => $faker->sentences,
'bio_html' => $faker->sentences,
'wesbite' => $faker->url,
'twitter' => $faker->word,
]);
}
}
}
我收到以下錯誤在終端當我嘗試和種子此表。
[Illuminate\Database\Eloquent\MassAssignmentException]
first_name
如果我嘗試和種子都我得到這個
[ErrorException]
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
我想包括Eloquent::unguard();
停止了這個錯誤?我正在運行最新版本的Laravel。
如果'DatabaseSeeder'類調用'Eloquent :: unguarded()',該解決方案爲何會有所幫助? – mike
它幫助了我。我使用單個播種機獲得相同的錯誤。 – klauskpm