Everything you need as a full stack developer

Eloquent Mass Assignment with fillable and guarded properties

- Posted in Laravel by

TL;DR As a Laravel developer, you're likely familiar with Eloquent, the powerful ORM tool that simplifies database interactions in your PHP applications. To ensure security and efficiency, use fillable and guarded properties to specify attributes allowed for mass assignment. The $fillable array allows updates of specified attributes, while the $guarded array protects sensitive attributes from mass assignment changes.

Mastering Eloquent Mass Assignment: A Guide to Fillable and Guarded Properties

As a Laravel developer, you're likely familiar with Eloquent, the powerful ORM (Object-Relational Mapping) tool that simplifies database interactions in your PHP applications. One of the key features of Eloquent is mass assignment, which allows you to update multiple attributes of an object at once. However, this feature also poses a security risk if not used carefully.

In this article, we'll delve into the world of Eloquent mass assignment and explore how to use fillable and guarded properties to ensure your applications are secure and efficient.

What is Mass Assignment?

Mass assignment is a process where you can update multiple attributes of an object by passing an array of values. This feature is convenient for creating, updating, or even deleting data in bulk. However, it also allows malicious users to update sensitive attributes that shouldn't be accessible via mass assignment.

The Problem with Mass Assignment

Let's say we have a User model with the following attributes:

protected $fillable = ['name', 'email'];

If an attacker can manipulate the request data sent to our controller, they might send something like this:

$user = User::find(1);
$user->update(['name' => 'John Doe', 'email' => 'john@example.com', 'admin' => true]);

In this scenario, the admin attribute is not supposed to be updated via mass assignment. But with the current setup, our application would silently update the admin flag to true, compromising its security.

Enter Fillable and Guarded Properties

To mitigate this risk, Laravel introduces two properties in Eloquent models: $fillable and $guarded.

  • $fillable: An array of attributes that are allowed to be mass-assigned. This means you can update these attributes using the update() method.
  • $guarded: An array of attributes that should not be mass-assigned.

Here's an updated example:

protected $fillable = ['name', 'email'];
protected $guarded = ['admin']; // prevent updating admin attribute via mass assignment

With this setup, if the attacker tries to update the admin attribute via mass assignment, Eloquent will silently ignore it and return an error.

Using Fillable and Guarded Properties Together

In many cases, you might need to allow some attributes to be updated via mass assignment while protecting others. In such scenarios, you can use a combination of $fillable and $guarded.

Here's an example where name, email, and password are allowed to be mass-assigned, but admin is not:

protected $fillable = ['name', 'email', 'password'];
protected $guarded = ['admin']; // prevent updating admin attribute via mass assignment

Best Practices for Eloquent Mass Assignment

To ensure your applications remain secure and efficient:

  1. Always use $fillable to specify the attributes allowed for mass assignment.
  2. Use $guarded to protect sensitive attributes from being updated via mass assignment.
  3. Keep the attributes in $fillable as small as possible, allowing only necessary updates.
  4. Validate user input before passing it to Eloquent's update() method.

By following these guidelines and mastering fillable and guarded properties, you'll be able to harness the power of Eloquent mass assignment while maintaining a secure and robust application.


This concludes our exploration of Eloquent mass assignment with fillable and guarded properties. We hope this article has provided valuable insights into securing your Laravel applications.

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more