MassAssignmentException in Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
MassAssignmentException is an error that occurs when trying to seed your database with multiple model attributes in Laravel. This issue arises from the security measure implemented by Laravel, which prevents mass assignment of attributes without explicitly defining them as fillable in the Eloquent model. In this blog post, we will cover how to fix MassAssignmentException issues and understand its underlying cause.
The Cause of MassAssignmentException
When creating models, Laravel uses a concept called mass assignment to allow for multiple attributes to be assigned to the model at once. This is helpful when seeding your database with large sets of data; however, Laravel has a security measure in place that prevents attackers from manipulating specific fields on a record without your consent. Therefore, by default, Laravel does not permit mass assignment of all attributes without declaring them as fillable.
Fixing MassAssignmentException Issues
The solution to overcoming MassAssignmentException issues lies in defining the attributes that can be assigned through mass assignment. There are several ways to achieve this, depending on your scenario.
1. Directly define fillable attributes: In your Eloquent model, add a fillable array containing all the attributes you want to allow for mass assignment. This can be done in the Model class, as shown below:class User extends Eloquent {
protected $fillable = [
'username',
'email',
'password'
];
}class User extends Eloquent {
protected $guarded = [];
}class User extends Eloquent {
protected $fillable = [];
public function __construct(array $attributes = []) {
parent::__construct($attributes);
if (isset($this->rawAttributes())) {
$this->fillable = array_keys($this->getAttributes());
}
}
}class UsersTableSeeder extends Seeder {
public function run() {
User::truncate();
User::create([
'username' => 'PaulSheer',
'email' => 'psheer@rute.co.za',
'password' => Hash::make('45678')
]);
User::create([
'username' => 'Stevo',
'email' => 'steve@rute.co.za',
'password' => Hash::make('12345')
]);
}
}Conclusion
MassAssignmentException is a common issue encountered while seeding databases in Laravel, but it can easily be fixed by understanding the underlying cause and following best practices. By defining fillable attributes or implementing custom logic in your Eloquent model, you can ensure a smooth seeding process and keep your application secure.