Mass Assignment Exception in Laravel - Fix
This error occurs when trying to mass assign attributes that aren't in the model's $fillable array.
The Error
MassAssignmentException - Add [field] to fillable property
Common Causes
- 1 Field not listed in $fillable array
- 2 Using create() or update() with unguarded fields
- 3 $guarded array blocking the field
Solutions
Add field to $fillable array
class Post extends Model
{
protected $fillable = [
'title',
'body',
'author_id',
];
}
Use $guarded instead for fewer restrictions
class Post extends Model
{
protected $guarded = ['id'];
}
Set attributes individually if needed
$post = new Post();
$post->title = $request->title;
$post->body = $request->body;
$post->save();
Need Help With Your Laravel Project?
I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.
Currently available for 2-3 new projects
About Hafiz
Full Stack Developer from Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.
View PortfolioRelated Errors
Column Cannot Be Null Error in Laravel - Fix
SQLSTATE[23000]: Integrity constraint vi...
ValidationException in Laravel - Handling Validation Errors
The given data was invalid - ValidationE...
Model Not Found Exception in Laravel - Solutions
ModelNotFoundException - No query result...