Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mass assignment error when seeding in laravel

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.

like image 624
joshuahornby10 Avatar asked Jan 17 '14 19:01

joshuahornby10


1 Answers

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());
like image 194
J.T. Grimes Avatar answered Oct 12 '22 23:10

J.T. Grimes