Mass assignment error when sowing in Laravel

I use the faker class to help sow my database. DatabaseSeeder is as follows:

<?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 UserTableSeeder

<?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 get the following error in the terminal when I try to sow this table.

[Illuminate\Database\Eloquent\MassAssignmentException]  
first_name      

If I try and run both, I get this

 [ErrorException]                                                                       
 preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

I thought turning on Eloquent::unguard();stopped this error? I am using the latest version of Laravel.

+4
source share
3 answers

faker->sentences()and faker->paragraphs()return the arrays , and your class expects to get a string.

You can use faker->text(), or you can use

implode(" ",$faker->sentences());
+11
source

;

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

, , DatabaseSeeder.php, :

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

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

Your UsersTableSeeder.php , for example:

<?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 User.php model add:

protected $guarded = [];

I performed here and both worked:

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

I configured laravel and faker on the composer, for example:

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

I hope he helps you.

+2
source

All Articles