Framework: Reguard model in seeding not working

Created on 25 Apr 2017  路  22Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.19

Description:

  • Trying to enable $fillable and $guard attributes on all model in seeding.
  • For that, trying to write Illuminate\Database\Eloquent\Model::reguard() in method run before start seeding in main class DatabaseSeeder.
  • And occur exception Illuminate\Database\Eloquent\MassAssignmentException on field, which present on $fillable attribute.

All 22 comments

Code to reproduce?

Code to reproduce

class SomeModel extends Illuminate\Database\Eloquent\Model
{
    protected $fillable = ['name'];
}

class DatabaseSeeder extends Illuminate\Database\Seeder
{
    public function run()
    {
        Illuminate\Database\Eloquent\Model::reguard();

        App\SomeModel::create([ 'id' => 100, 'name' => 'Foo' ]);
    }
}

After run php artisan db:seed i have exception Illuminate\Database\Eloquent\MassAssignmentException (name)

You're setting 'name' to be the only attribute that you want to allow to be mass assigned, then setting $unguarded to false, which means mass assignment will be checked. Then you try to mass assign 'id', which throws a mass assignment exception. Seems everything is working properly.
Maybe Model::unguard() is what you're looking for?

I just want to enable MassAssignment protection, that disabled by default in seeding (static::$unguarded = true;).
And i trying enable this by calling Model::reguard() for set property $unguarded = false (in docs: Enable the mass assignment restrictions.)
And strange, i got exception on not field id (because it not present in $fillable).

@Dylan-DPC Its not working...

can you share some code that you have tried with Model::unguard()?

Dude, see example above)

I use Model::reguard() method to enable mass assignment restrictions http://joxi.ru/l2ZVP4EHKDb42J

My bad. Can you try it with another test case?

For what? This example describe main meaning.
Try to use and u will see )

I have just tried the same and it works as expected.

I have error http://joxi.ru/nAyXNvoT8gZk2Z 馃槶

Looks like the core code actually wraps the call to each seeder in a closure that automatically unguards the seeding. So if you need to unguard each seeder, then your reguard method would need to be called just inside the run method of each seeder class that you want guarded. Just a guess, but scanning over the code for db:seed, it seems plausible.

Your code is working exactly like it should. Mass assignment is enabled and working properly when you call 'reguard'. What do you expect to happen? By default, in seeders, mass-assignment is bypassed. When you add 'reguard', then an exception will be thrown if you try to assign a property that is not listed in your 'fillable' fields. Seems like you're not understanding what mass-assignment protection is.

Either way, since this doesn't appear to be a bug, it would probably be best to take it to larachat or [laracasts forums[(laracasts.com/discuss).

But attribute name exists in property $fillable on model!

Can't reproduce. Hopefully someone will come along with some insight.

@devcircus Thanks for assistance 馃檪

Test repo: https://github.com/RikSomers/SeederTest

I followed your steps:

  • Make the model with migration (php artisan make:model SomeModel -m)
  • Change migration to contain name field
  • Fill the fillable array with 'name'
  • Run migrations (php artisan migrate)
  • Copy your seeder code
  • Run seeder (php artisan db:seed)

Can't reproduce the error you're getting.

Do you have some other application code that might be executed?

For example, you might have another model that gets created in the process of the seeder, which is given a 'name' attribute that is not mass assignable.

Big sorry for all, my fault, i was looking on different model, where not present $fillable property 馃

No worries. We all make mistakes!

Best you can do in these cases is try to create the most simple testcase (like I did) and see if you can reproduce the error. If not, it's probably something in your own code you missed, and those issues are more suitable for the forums.
Otherwise, you already have the steps to reproduce right there in your small testcase repo!

Yea, i understand, thanks dude 馃槂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerbylav picture kerbylav  路  3Comments

felixsanz picture felixsanz  路  3Comments

ghost picture ghost  路  3Comments

iivanov2 picture iivanov2  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments