Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 28 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

Preventing Mass Assignment in Laravel: Secure Your Models

Learn how to prevent mass assignment vulnerabilities in Laravel by using the $fillable property to secure your Eloquent models from malicious input.

laravelphpbackend

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.

The Risk of 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."

Securing Models with $fillable

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.

Worked Example: Protecting the 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:

PHP
namespace 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.

When to use $guarded?

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).

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

Hands-on Exercise

  1. Open your Task model in the app/Models directory.
  2. Add the title and description fields to the $fillable array.
  3. If you haven't already, ensure your tasks table migration includes these columns.
  4. Try to "hack" your own application by sending a hidden input field in your task creation form (e.g., <input type="hidden" name="user_id" value="999">).
  5. Check your database after submission; you will see that the user_id remains unchanged or follows your business logic, rather than the malicious input.

Common Pitfalls

  • Forgetting to update the array: Many developers add a new column to the migration and then spend an hour debugging why the data isn't saving. Always check the $fillable property when you add new database columns.
  • Over-sharing: Don't just put all columns in $fillable to "make it work." Only include fields that the user is actually allowed to modify via a form.
  • Mixing $fillable and $guarded: Laravel will throw an error if you define both on the same model. Choose one strategy and stick to it throughout your project.

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.

Recap

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.

Previous lessonUnderstanding CSRF ProtectionNext lesson Task Manager: Securing the Application
Back to Blog

Similar Posts

LaravelJune 25, 20263 min read

Seeding the Database: A Beginner’s Guide to Laravel Factories

Learn how to use database seeding and factories in Laravel to populate your application with realistic dummy data for testing and development.

Read more
LaravelJune 25, 20263 min read

Creating Your First Controller: Mastering Request Handling

Learn how to use controllers to clean up your Laravel routes, organize your code using the MVC pattern, and handle incoming requests like a pro.

Part of the course

Laravel Fundamentals: From Zero to Your First App

beginner · Lesson 28 of 52

  1. 1

    Setting Up the Local Development Environment

    4 min
  2. 2

    Installing Laravel and Exploring Directory Structure

    3 min
  3. 3

    Understanding the .env File and Configuration

    3 min
Read more
LaravelPHPJune 24, 20264 min read

Eloquent custom casts: A Beginner’s Guide to Transforming Data

Eloquent custom casts let you clean up your Laravel models by automatically transforming data. Learn how to handle complex types without the boilerplate.

Read more
  • 4

    The Laravel Application Lifecycle

    4 min
  • 5

    Initializing the Task Manager Project

    3 min
  • 6

    Defining Basic Web Routes

    4 min
  • 7

    Using Route Parameters

    3 min
  • 8

    Creating Your First Controller

    3 min
  • 9

    Returning Responses and Redirects

    3 min
  • 10

    Task Manager: Implementing the Task List Route

    3 min
  • 11

    Introduction to Blade Templating

    3 min
  • 12

    Using Blade Layouts and Sections

    3 min
  • 13

    Implementing Blade Partials

    4 min
  • 14

    Mastering Blade Directives for Loops and Conditionals

    3 min
  • 15

    Task Manager: Building the User Interface

    3 min
  • 16

    Understanding Database Migrations

    3 min
  • 17

    Working with Eloquent Models

    3 min
  • 18

    Performing Basic CRUD Operations

    3 min
  • 19

    Seeding the Database

    3 min
  • 20

    Task Manager: Displaying Real Database Records

    3 min
  • 21

    Capturing User Input from Forms

    4 min
  • 22

    Introduction to Laravel Validation

    3 min
  • 23

    Customizing Validation Error Messages

    3 min
  • 24

    Using Form Requests for Validation

    3 min
  • 25

    Introduction to Authentication

    4 min
  • 26

    Protecting Routes with Middleware

    3 min
  • 27

    Understanding CSRF Protection

    3 min
  • 28

    Preventing Mass Assignment

    3 min
  • 29

    Task Manager: Securing the Application

    3 min
  • 30

    Introduction to Route Model Binding

    3 min
  • 31

    Updating Existing Records

    3 min
  • 32

    Deleting Records

    3 min
  • 33

    Using Named Routes

    3 min
  • 34

    Task Manager: Completing CRUD Functionality

    Coming soon
  • 35

    Introduction to Database Relationships

    Coming soon
  • 36

    Querying Related Data

    Coming soon
  • 37

    Handling File Uploads

    Coming soon
  • 38

    Using Flash Messages for User Feedback

    Coming soon
  • 39

    Task Manager: Adding Status and Priorities

    Coming soon
  • 40

    Introduction to Artisan Commands

    Coming soon
  • 41

    Debugging with Laravel Tinker

    Coming soon
  • 42

    Understanding Service Providers

    Coming soon
  • 43

    Using View Composers

    Coming soon
  • 44

    Task Manager: Refactoring for Clean Code

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Testing Forms and Validation

    Coming soon
  • 47

    Using Database Transactions

    Coming soon
  • 48

    Handling Global Exceptions

    Coming soon
  • 49

    Preparing for Production

    Coming soon
  • 50

    Environment Security Best Practices

    Coming soon
  • 51

    Managing Assets in Production

    Coming soon
  • 52

    Task Manager: Deployment Preparation

    Coming soon
  • View full course