MassAssignmentException in Laravel

MassAssignmentException in Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

MassAssignmentException in Laravel

Encountering a MassAssignmentException during database seeding is one of the most common friction points for developers moving from raw SQL or early Laravel versions to modern Eloquent models. The error you are seeing, specifically regarding the username attribute, indicates that your model is actively preventing data from being written to the database under the circumstances you specified. This behavior is not a bug; it is a deliberate security feature designed to mitigate Mass Assignment Vulnerabilities, where an attacker might inject hidden fields into a form to escalate privileges or alter system state.

When you execute php artisan db:seed --class="UsersTableSeeder", the User::create() method attempts to populate the database with the provided array. However, Eloquent checks this array against the model's fillable attributes before executing any SQL. If the username key exists in your input array but is not explicitly allowed by the model, Eloquent throws this exception to halt the operation immediately.

Your seed class attempts to create users with a username, email, and password. The error log points specifically to username. This means that somewhere in your User model definition, you have likely restricted which attributes can be mass-assigned. In older Laravel versions (prior to 5.6) or if using the default configuration without modification, all fillable fields were allowed by default, but modern installations often enforce strict rules out of the box.

To resolve this, you must inspect your User model file. You will likely find a $fillable property defined as an empty array:

class User extends Model
{
    protected $fillable = [];
}

By setting $fillable to an empty array, the developer has opted into "strict" mass assignment mode. In this mode, only attributes explicitly listed in that array can be set via create() or update(). Since username is not in that list, Eloquent blocks the write operation and throws the exception you observed.

The solution is straightforward: add username to the $fillable property. You should also consider adding email, as it is standard practice to allow these core user attributes for seeding purposes. Update your model accordingly:

protected $fillable = ['username', 'email', 'password'];

Once this change is applied, re-run your seeder command. The exception will vanish, and the users will be created successfully.

It is important to understand the trade-off here. While enabling these fields allows you to seed data easily, it opens a security gap for any other endpoint that accepts user input. If you have a registration form or an API controller that accepts a username field, you must ensure that the controller explicitly validates and sanitizes that input before passing it to the model. Never rely solely on $fillable to secure your application; always validate incoming requests.

If you prefer not to modify the model globally—for instance, if you are working with a shared library or a strict framework where models cannot be altered—you can disable mass assignment protection entirely by setting the APP_PREVENT_STRICT_MODE configuration option (in newer Laravel versions) or adding the --no-strategy flag depending on your specific setup. However, in most standard projects, modifying $fillable is the preferred approach because it maintains a clear separation between trusted seeding data and untrusted user input.

Remember that password is also sensitive. Even if you add it to $fillable, ensure you are hashing the password using Hash::make() before passing it to User::create(). Storing plain-text passwords like '45678' in your database is a critical security risk regardless of mass assignment settings.

For further guidance on securing your application and understanding attribute protection, resources like https://laravelcompany.com offer deeper dives into Eloquent configuration and best practices for handling user data safely.

Our network