Learn how to prevent mass assignment vulnerabilities in Laravel by using the $fillable property to secure your Eloquent models from malicious input.
Previously in this course, we explored understanding CSRF protection to ensure our forms are legitimate. In this lesson, we add another layer of defense: preventing mass assignment.
When you build a web application, you often want to take the data submitted from a form and save it directly into the database. Eloquent makes this incredibly easy with methods like create() or update().
However, this convenience comes with a major security risk. Suppose your users table has an is_admin column. If you blindly pass the entire request object to the database, a malicious user could inject is_admin=1 into their HTTP request. If your application code accepts all input, the user just promoted themselves to an administrator. This is the essence of a mass assignment vulnerability.
Eloquent is designed to be "secure by default" regarding this. It ignores any input that isn't explicitly allowed to be "mass assigned."
To tell Eloquent which attributes are safe to be updated by users, we use the $fillable property inside our model. This acts as a whitelist. Any field not in this array will be ignored during mass assignment, effectively neutralizing the risk of unauthorized data modification.
Let’s apply this to our Task model.
In our Task Manager project, a user should be able to create a task by providing a title and a description. We do not want them to be able to set the is_completed status or change the user_id arbitrarily.
Open app/Models/Task.php and define the $fillable array:
PHPnamespace App\Models; use Illuminate\Database\Eloquent\Model; class Task extends Model { #6A9955">/** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'title', 'description', ]; }
Now, if a user attempts to send a request containing is_completed => true alongside their title, Eloquent will process the title and description but silently ignore the is_completed field.
Alternatively, you can use the $guarded property. While $fillable is a whitelist (only these fields are allowed), $guarded is a blacklist (these fields are blocked).
PHPprotected $guarded = ['id', 'is_admin'];
I personally prefer $fillable because it forces you to be explicit about what your application expects. If you add a new column to your database, it remains protected by default until you deliberately add it to your $fillable array.
Task model in the app/Models directory.title and description fields to the $fillable array.tasks table migration includes these columns.<input type="hidden" name="user_id" value="999">).user_id remains unchanged or follows your business logic, rather than the malicious input.$fillable property when you add new database columns.$fillable to "make it work." Only include fields that the user is actually allowed to modify via a form.While this approach covers basic model security, advanced architectures often move toward more complex patterns for data validation and transfer, as discussed in our deep dives into preventing mass assignment with DTOs. By mastering the basics of Eloquent security now, you're building a solid foundation for more robust, production-grade systems.
Mass assignment occurs when your application accepts broad user input and saves it directly into the database. By using the $fillable property in your Eloquent models, you create a whitelist that ensures only safe, expected data is persisted. This simple configuration is your first line of defense against unauthorized attribute modification.
Up next: Task Manager: Securing the Application.
Learn how to use database seeding and factories in Laravel to populate your application with realistic dummy data for testing and development.
Read moreLearn how to use controllers to clean up your Laravel routes, organize your code using the MVC pattern, and handle incoming requests like a pro.
Preventing Mass Assignment
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation