tprsn
tprsn

Reputation: 767

Laravel seeding mass-assignment, can't see where I'm going wrong

In my seeder:

    $model->create(array(
        'name' => 'Test name'
    ));

In my model:

class Model extends Content {
        protected $fillable = array('name');
}

But when I run the create method, the record just gets created in the database with NULL in the name field.

What am I doing wrong?

Upvotes: 0

Views: 410

Answers (4)

tprsn
tprsn

Reputation: 767

Found the solution to this.

Model was extending Content which extends Eloquent.

I was overriding the constructor in Content, and not passing the $attributes array to the Eloquent constructor. I needed to change this:

class Content extends Eloquent {
    public function __construct() {
        ...
        parent::__construct();
    }
    ...
}

To this:

class Content extends Eloquent {
public function __construct($attributes = array()) {
        ...
        parent::__construct($attributes);
}
    ...
}

Upvotes: 1

majidarif
majidarif

Reputation: 20105

Try using this on your model instead:

class Model extends Content {
    protected $fillable = ['name'];
}

to remove the un-necessary code.

Or on your seeder try:

Model::create(['name' => 'foobar']);

Or add this on your seeder:

Eloquent::unguard();

Upvotes: 0

Fed03
Fed03

Reputation: 585

You are specifing both the fillable and the guarded properties. You only need one of them!

Upvotes: 0

Bradley Weston
Bradley Weston

Reputation: 425

In your seed you will need to "unguard" Eloquent.

https://github.com/laravel/framework/issues/736#issuecomment-15597996

Upvotes: 0

Related Questions