I am using the faker class to help seeder my database. The DatabaseSeeder looks like this
<?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');
}
}
and the 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,
]);
}
}
}
I am getting the following error in the terminal when I try and seed this table.
[Illuminate\Database\Eloquent\MassAssignmentException]
first_name
If I try and seed both I get this
[ErrorException]
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
I thought including Eloquent::unguard();
stopped this error? I am running the latest version of Laravel.
faker->sentences()
and faker->paragraphs()
return arrays and your class expects to receive a string.
You can either use faker->text()
or you can you can use
implode(" ",$faker->sentences());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With