Framework: Seed database with mass assignment protection enabled

Created on 27 Mar 2013  Â·  10Comments  Â·  Source: laravel/framework

With the latest change

Protect against mass assignment by default.

I'm curious on how to seed the database with mass assignment protection enabled?

In Rails, there is an option :without_protection to bypass the mass assignment security.

What is the recommended way to do this with Laravel?

Most helpful comment

This is done. There is a new Eloquent::unguard method to disable all mass assignment restrictions. I have added it to the top of the DatabaseSeeder that ships with Laravel. So you can guys can add it to the top of your main seeder.

All 10 comments

Add as a property in your model class:

protected $guarded = array();

It is array('*') by default, and by removing the asterisk you remove the protection.

I think his point is sometimes its convenient to genuinely mass assign even protected attributes in the context of seeding.

On Mar 27, 2013, at 5:44 PM, Joost Koehoorn [email protected] wrote:

Add as a property in your model class:

protected $guarded = array();
It is array('*') by default, so by removing the asterisk you remove the protection.

—
Reply to this email directly or view it on GitHub.

Ah, missed that line :)

I remember you having proposed a syntax like:

$model->fillable('*')->fill($data);

So you can alter the protection right from your controllers. For seeders this should be done a little differently, but it may use the same method under the hood.

Exactly. I want to keep the mass assignment protection but be able to bypass it for certain situations (like database seeding). I like the way Rails solved this.

+1

I think that mass assignment protection was removed. See #697 and a4db9c5baf218c97fea4f8c850127803b8b23f49

Nope, it's still there.

This is done. There is a new Eloquent::unguard method to disable all mass assignment restrictions. I have added it to the top of the DatabaseSeeder that ships with Laravel. So you can guys can add it to the top of your main seeder.

This is awesome, thank you!

@taylorotwell actually I don't know why we need to disable the assignment restrictions. I'm working with Service when I create,edit,delete and the same service is been used in the Seeding process as well what cuz to errors when the Eloquent try to insert non-existed fields.

Double work for me when I create the seeders, maybe for the other it is OK.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  Â·  3Comments

JamborJan picture JamborJan  Â·  3Comments

klimentLambevski picture klimentLambevski  Â·  3Comments

RomainSauvaire picture RomainSauvaire  Â·  3Comments

digirew picture digirew  Â·  3Comments