Home » Php » Mass assignment error when seeding in laravel

Mass assignment error when seeding in laravel

Posted by: admin November 29, 2017 Leave a comment

Questions:

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.

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());

Questions:
Answers:

You can define empty array of guarded fields in your model;

class User extends Eloquent
{
    protected $guarded = []; 
}

Questions:
Answers:

Well, i think you just need to do simple in your DatabaseSeeder.php, like this:

public function run()
{
  Eloquent::unguard();

  $this->call('UsersTableSeeder');
  $this->call('PostsTableSeeder');
}

Your UsersTableSeeder.php, like these:

<?php

use Faker\Factory as Faker;

class UsersTableSeeder extends Seeder {

  public function run()
  {
    $faker = Faker::create();

    for( $i=0 ; $i<50 ; $i++ ) {
      $user = User::create([
        'first_name'         => $faker->firstName, // try using str_random(10)
        'surname'            => $faker->lastName,  // try using str_random(20)
        'email'              => $faker->email,
        'username'           => $faker->userName,  // $faker->unique()->userName
        'bio'                => $faker->sentences,
        'bio_html'           => $faker->sentences,
        'wesbite'            => $faker->url,
        'twitter'            => $faker->word,
      ]);
    }
  }
}

And, in your model, User.php, add:

protected $guarded = [];

I executed here and both worked:

php artisan db:seed
php artisan db:seed --class=UsersTableSeeder

I configured laravel and faker, on composer, like these:

"require": {
  "laravel/framework": "4.2.*",
  "fzaninotto/faker": "dev-master"
},

Hope it help you.