Mass Assignment Protection in Laravel: Why fillable Matters
When creating a new Post using the Eloquent ORM, you might encounter an error like this: "Add [title] to the fillable property to allow mass assignment on [App\Post]." This happens when your code attempts to instantiate a model and assign attributes in a single call, such as $post = Post::create([...]). While creating the object manually via new Post followed by direct property assignment works without issues, Laravel blocks the first approach by default.
This behavior is not a bug; it is a critical security feature known as mass assignment protection. The core issue lies in how Eloquent handles bulk attribute setting versus individual property access. When you use methods like create() or fill(), Eloquent executes a single database insertion statement that maps multiple input fields to model attributes simultaneously. This efficiency comes with a risk: if your controller receives user input containing unexpected keys, those values could silently overwrite sensitive database columns like updated_at or boolean flags without your knowledge.
Laravel mitigates this by restricting which properties are allowed to be set through mass assignment. You define this list in the $fillable property of your model. Without it, Eloquent assumes that any input provided via an array is potentially malicious and refuses to execute the update. This explains why the second code snippet works fine; you are assigning title and body directly to the object's properties before saving. These direct assignments bypass the mass assignment protection logic because they do not process a raw array of attributes against the model's whitelist.
To resolve the error, you must explicitly define which columns are safe to accept from external input. In your Post model, add the following property:
class Post extends Model
{
protected $fillable = ['title', 'body'];
}
By declaring 'title' and 'body' in the $fillable array, you tell Eloquent that these are the only attributes allowed to be set via mass assignment operations. Once this is configured, your original Post::create() call will execute successfully because the input keys now match the protected list.
It is important to note that if a user sends data with a key outside of $fillable, such as 'is_admin' or 'deleted_at', Eloquent will simply ignore those keys during the mass assignment process. They are not stored in the database, preventing accidental privilege escalation or data corruption. However, remember that columns listed in $fillable can still be updated manually via direct property assignment or query scopes. Mass assignment protection only restricts bulk updates from arrays, not individual field modifications.
If you prefer a more restrictive approach where all attributes are protected by default, you can use the $guarded array instead. If $guarded is empty, every column is considered fillable. Conversely, if you want to guard specific columns like updated_at, you would define them in $guarded. This trade-off allows you to either whitelist safe fields or blacklist sensitive ones, depending on your application's security posture and the nature of your data model.
For more details on securing your application against mass assignment vulnerabilities, refer to the official Laravel documentation. If you are looking for additional resources or community support on implementing secure practices in Laravel projects, you might also explore guides from https://laravelcompany.com.