Eloquent Laravel 8 Laravel 9 Laravel 10 Laravel 11 Laravel 12

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

Error Message
MassAssignmentException - Add [field] to fillable property

Common Causes

  1. 1 Field not listed in $fillable array
  2. 2 Using create() or update() with unguarded fields
  3. 3 $guarded array blocking the field

Solutions

1

Add field to $fillable array

PHP
class Post extends Model
{
    protected $fillable = [
        'title',
        'body',
        'author_id',
    ];
}
2

Use $guarded instead for fewer restrictions

PHP
class Post extends Model
{
    protected $guarded = ['id'];
}
3

Set attributes individually if needed

PHP
$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

Hafiz Riaz

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 Portfolio

Related Errors