framework: Reguard model in seeding not working
- 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 methodrun
before start seeding in main classDatabaseSeeder
. - And occur exception
Illuminate\Database\Eloquent\MassAssignmentException
on field, which present on$fillable
attribute.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 22 (4 by maintainers)
Test repo: https://github.com/RikSomers/SeederTest
I followed your steps:
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.
But attribute
name
exists in property$fillable
on model!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).
I use
Model::reguard()
method to enable mass assignment restrictions http://joxi.ru/l2ZVP4EHKDb42JCode to reproduce
After run
php artisan db:seed
i have exceptionIlluminate\Database\Eloquent\MassAssignmentException (name)