Laravel’s safety mechanisms
By Aaron Francis |
Laravel is a mature PHP web application framework with built-in support for almost everything modern applications need. But we’re not going to cover all of those features here! Instead, we’ll look at a topic that doesn’t get talked about nearly enough: Laravel’s many safety features that can help prevent painful mistakes.
We’ll take a look at the following safety mechanisms:
- N+1 prevention
- Partially hydrated model protection
- Attribute typos and renamed columns
- Mass assignment protection
- Model strictness
- Polymorphic mapping enforcement
- Long-running event monitoring
Each of these protections is configurable, and we’ll recommend how and when to configure them.
N+1 prevention
Many ORMs, Eloquent included, offer a “feature“ that allows you to lazy load a model’s relationship. Lazy loading is convenient because you don’t have to think upfront about which relationships to select from the database, but it often leads to a performance nightmare known as the “N+1 problem.”
The N+1 problem is one of the most common problems people run into when using an ORM, and it’s often a reason people cite for avoiding ORMs altogether. That’s a bit of an overcorrection, as we can simply disable lazy loading altogether!
Imagine a naive listing of blog posts. We’ll show the blog’s title and the author’s name.
$posts = Post::all(); foreach($posts as $post) { // `author` is lazy loaded. echo $post->title . ' - ' . $post->author->name; }
This is an example of the N+1 problem! The first line selects all of the blog posts. Then, for every single post, we run another query to get the post’s author.
SELECT * FROM posts; SELECT * FROM users WHERE user_id = 1; SELECT * FROM users WHERE user_id = 2; SELECT * FROM users WHERE user_id = 3; SELECT * FROM users WHERE user_id = 4; SELECT * FROM users WHERE user_id = 5;
The “N+1“ notation comes from the fact that an additional query is run for each of the n-many records returned by the first query. One initial query plus n-many more. N+1.
Even though each individual query is probably quite fast, in aggregate, you can see a huge performance penalty. And because each individual query is fast, this isn’t something that would show up in your slow query log!
With Laravel, you can use the preventLazyLoading
method on the Model
class to disable lazy loading altogether. Problem solved! Truly, it is that simple.
You can add the method in your AppServiceProvider
:
use Illuminate\Database\Eloquent\Model; public function boot() { Model::preventLazyLoading(); }
Every attempt to lazy load a relationship will now throw a LazyLoadingViolationException
exception. Instead of lazy loading, you’ll need to explicitly eager load your relationships.
// Eager load the `author` relationship. $posts = Post::with('author')->get(); foreach($posts as $post) { // `author` is already loaded. echo $post->title . ' - ' . $post->author->name; }
Lazy loading relationships does not affect the correctness of your application, merely the performance of it. Ideally, all the relations you need are eager loaded, but if not, it simply falls through and lazy loads the required relationships.
For that reason, we recommend disallowing lazy loading in every environment except production. Hopefully, all lazy loads will be caught in local development or testing, but in the rare case that a lazy load makes its way into production, your app will continue to work just fine, if a bit slower.
To prevent lazy loading in non-production environments, you can add this to your AppServiceProvider
:
use Illuminate\Database\Eloquent\Model; public function boot() { // Prevent lazy loading, but only when the app is not in production. Model::preventLazyLoading(!$this->app->isProduction()); }
If you want to log errant lazy loading in production, you can register your own lazy load violation handler using the static handleLazyLoadingViolationUsing
method on the Model class.
In the example below, we will disallow lazy loading in every environment, but in production, we log the violation rather than throwing an exception. This ensures that our application continues to work as intended, but we can go back and fix our lazy load mistakes.
use Illuminate\Database\Eloquent\Model; public function boot() { // Prevent lazy loading always. Model::preventLazyLoading(); // But in production, log the violation instead of throwing an exception. if ($this->app->isProduction()) { Model::handleLazyLoadingViolationUsing(function ($model, $relation) { $class = get_class($model); info("Attempted to lazy load [{$relation}] on model [{$class}]."); }); } }
Partially hydrated model protection
In almost every book about SQL, one of the performance recommendations that you’ll see is to “select only the columns that you need.” It’s good advice! You only want the database to fetch and return the data that you’re actually going to use because everything else is simply discarded.
Until recently, this has been a tricky (and sometimes dangerous!) recommendation to follow in Laravel.
Laravel’s Eloquent models are an implementation of the active record pattern, where each instance of a model is backed by a row in the database.
To retrieve the user with an ID of 1, you can use Eloquent’s User::find()
method, which runs the following SQL query:
SELECT * FROM users WHERE id = 1;
Your model will be fully hydrated, meaning that every column from the database will be present in the in-memory model representation:
$user = User::find(1); // -> SELECT * FROM users where id = 1; // Fully hydrated model, every column is present as an attribute. // App\User {#5522 // id: 1, // name: "Aaron", // email: "aaron@example.com", // is_admin: 0, // is_blocked: 0, // created_at: "1989-02-14 08:43:00", // updated_at: "2022-10-19 12:45:12", // }
Selecting all of the columns, in this case, is probably fine! But if your users
table is extremely wide, has LONGTEXT
or BLOB
columns, or you’re selecting hundreds or thousands of rows, you probably want to limit the columns to just the ones you plan on using. (Watch our schema videos to learn more about the LONGTEXT and BLOB columns and why you should avoid selecting them if you don't need them.)
You can control which columns are selected using the select
method, which leads to a partially hydrated model. The in-memory model contains a subset of attributes from the row in the database.
$user = User::select('id', 'name')->find(1); // -> SELECT id, name FROM users where id = 1; // Partially hydrated model, only some attributes are present. // App\User { // id: 1, // name: "Aaron", // }
Here’s where things get dangerous.
If you access an attribute that was not selected from the database, Laravel simply returns null
. Your code will think an attribute is null
, but really it just wasn’t selected from the database. It might not be null
at all!
In the following example, a model is partially hydrated with only id
and name
, then the is_blocked
attribute is accessed further down. Because is_blocked
was never selected from the database, the attribute’s value will always be null
, treating every blocked user as if they aren’t blocked.
// Partially hydrate a model. $user = User::select('id', 'name')->find(1); // is_blocked was not selected! It will always be `null`. if ($user->is_blocked) { throw new \Illuminate\Auth\Access\AuthorizationException; }
This exact example probably (probably) wouldn’t happen, but when data retrieval and usage are spread across multiple files, something like this will happen. There is no warning anywhere that a model is partially hydrated, and as requirements evolve, you may end up accessing attributes that were never loaded.
With extreme care and 100% test coverage, you might be able to prevent this from ever happening, but it’s still a loaded gun pointed straight at your foot. For that reason, we’ve recommended never modifying the SELECT
statement that populates an Eloquent model.
Until now!
The release of Laravel 9.35.0 brings us a new safety feature to prevent this from happening.
In 9.35.0 you can call Model::preventAccessingMissingAttributes()
to prevent accessing attributes that were not loaded from the database. Instead of returning null
, an exception will be thrown, and everything will grind to a halt. This is a very good thing.
You can enable this new behavior by adding this to your AppServiceProvider
:
use Illuminate\Database\Eloquent\Model; public function boot() { Model::preventAccessingMissingAttributes(); }
Notice that we enabled this protection across the board, regardless of environment! You could enable this protection only in local development, but the most important place for it to be enabled is production.
Unlike N+1 protection, preventing access to missing attributes is not a performance issue, it’s an application correctness issue. Enabling it prevents your application from behaving in unexpected and incorrect ways.
Accessing attributes that weren’t selected could lead to all sorts of catastrophic behavior:
- Data loss
- Overwriting data
- Treating free users as paid
- Treating paid users as free
- Sending factually incorrect emails
- Sending the same email dozens of times
The list goes on and on.
While throwing exceptions in production is inconvenient, it’s much worse to have silent failures that could lead to data corruption. Better to face the exceptions and fix them.
Attribute typos and renamed columns
This is a continuation of the previous section and another plea to turn on Model::preventAccessingMissingAttributes()
in your production environments.
We just spent a long time looking at how preventAccessingMissingAttributes()
protects you from partially hydrated models, but there are two other scenarios where this method can protect you!
The first is typos.
Continuing with the is_blocked
scenario from above, if you accidentally misspell “blocked,” Laravel will just return null
instead of letting you know about your mistake.
// Fully hydrated model. $user = User::find(1); // Oops! Spelled "blocked" wrong. Everyone gets through! if ($user->is_blokced) { throw new \Illuminate\Auth\Access\AuthorizationException; }
This particular example would likely be caught in testing, but why risk it?
The second scenario is renamed columns. If your column started out named blocked
and then later you decide it makes more sense for it to be named is_blocked
, you’d need to make sure to go back through your code and update every reference to blocked
. And if you miss one? It just becomes null
.
// Fully hydrated model. $user = User::find(1); // Oops! Used the old name. Everyone gets through! if ($user->blocked) { throw new \Illuminate\Auth\Access\AuthorizationException; }
Turning on Model::preventAccessingMissingAttributes()
would turn this silent failure into an explicit one.
Mass assignment protection
A mass assignment is a vulnerability that allows users to set attributes that they shouldn’t be allowed to set.
For example, if you have an is_admin
property, you don’t want users to be able to arbitrarily upgrade themselves to an admin! Laravel prevents this by default, requiring you to explicitly allow attributes to be mass assigned.
In this example, the only attributes that can be mass assigned are name
and email
.
class User extends Model { protected $fillable = [ 'name', 'email', ]; }
It doesn’t matter how many attributes you pass in when creating or saving the model. Only name
and email
will get saved:
// It doesn’t matter what the user passed in, only `name` // and `email` are updated. `is_admin` is discarded. User::find(1)->update([ 'name' => 'Aaron', 'email' => 'aaron@example.com', 'is_admin' => true ]);
Many Laravel developers opt to turn off mass assignment protection altogether and rely on request validation to exclude attributes. That’s totally reasonable! You just need to ensure you never pass $request->all()
into your model persistence methods.
You can add this to your AppServiceProvider
to turn off mass assignment protection altogether.
use Illuminate\Database\Eloquent\Model; public function boot() { // No mass assignment protection at all. Model::unguard(); }
Remember: you’re taking a risk when you unguard your models! Be sure to never blindly pass in all of the request data.
// Only update `name` and `email`. User::find(1)->update($request->only(['name', 'email']));
If you decide to keep mass assignment protection on, there is one other method that you’ll find helpful: the Model::preventSilentlyDiscardingAttributes()
method.
In the case where your fillable attributes are only name
and email
, and you try to update birthday
, then birthday
will be silently discarded with no warning.
// We’re trying to update `birthday`, but it won’t persist! User::find(1)->update([ 'name' => 'Aaron', 'email' => 'aaron@example.com', 'birthday' => '1989-02-14' ]);
The birthday
attribute gets thrown away because it’s not fillable. This is mass assignment protection in action, and it’s what we want! It’s just a little bit confusing because it’s silent instead of explicit.
Laravel now provides a way to make that silent error explicit:
use Illuminate\Database\Eloquent\Model; public function boot() { // Warn us when we try to set an unfillable property. Model::preventSilentlyDiscardingAttributes(); }
Instead of silently discarding the attributes, a MassAssignmentException
will be thrown, and you’ll immediately know what’s happening.
This protection is very similar to the preventAccessingMissingAttributes
protection. It is primarily about application correctness versus application performance. If you’re expecting that data is saved, but it is not saved, that’s an exception and should never be silently ignored, regardless of environment.
For that reason, we recommend keeping this protection on in all environments!
use Illuminate\Database\Eloquent\Model; public function boot() { // Warn us when we try to set an unfillable property, // in every environment! Model::preventSilentlyDiscardingAttributes(); }
Model strictness
Laravel 9.35.0 provides a helper method called Model::shouldBeStrict()
that controls the three Eloquent “strictness” settings:
Model::preventLazyLoading()
Model::preventSilentlyDiscardingAttributes()
Model::preventsAccessingMissingAttributes()
The idea here is that you could put the shouldBeStrict()
call in your AppServiceProvider
and turn all three settings on or off with one method call. Let’s quickly recap our recommendations for each setting:
preventLazyLoading
: Primarily for application performance. Off for production, on locally. (Unless you’re logging violations in production.)preventSilentlyDiscardingAttributes
: Primarily for application correctness. On everywhere.preventsAccessingMissingAttributes
: Primarily for application correctness. On everywhere.
Considering this, if you’re planning on logging lazy loading violations in production, you could configure your AppServiceProvider
like this:
use Illuminate\Database\Eloquent\Model; public function boot() { // Everything strict, all the time. Model::shouldBeStrict(); // In production, merely log lazy loading violations. if ($this->app->isProduction()) { Model::handleLazyLoadingViolationUsing(function ($model, $relation) { $class = get_class($model); info("Attempted to lazy load [{$relation}] on model [{$class}]."); }); } }
If you're not planning on logging lazy load violations (which is a reasonable decision!), then you would configure your settings this way:
use Illuminate\Database\Eloquent\Model; public function boot() { // As these are concerned with application correctness, // leave them enabled all the time. Model::preventAccessingMissingAttributes(); Model::preventSilentlyDiscardingAttributes(); // Since this is a performance concern only, don’t halt // production for violations. Model::preventLazyLoading(!$this->app->isProduction()); }
Polymorphic mapping enforcement
A polymorphic relationship is a special type of relationship that allows many types of parent models to share a single type of child model.
For example, a blog post and a user may both have images, and instead of creating a separate image model for each, you can create a polymorphic relationship. This lets you have a single Image
model that serves both the Post
and User
models. In this example, the Image
is the polymorphic relationship.
In the images
table, you’ll see two columns that Laravel uses to locate the parent model: an imageable_type
and an imageable_id
column.
The imageable_type
column stores the model type in the form of the fully qualified class name (FQCN), and the imageable_id
is the model's primary key.
mysql> select * from images; +----+-------------+-----------------+------------------------------+ | id | imageable_id | imageable_type | url | +----+-------------+-----------------+------------------------------+ | 1 | 1 | App\Post | https://example.com/1001.jpg | | 2 | 2 | App\Post | https://example.com/1002.jpg | | 3 | 3 | App\Post | https://example.com/1003.jpg | | 4 | 22001 | App\User | https://example.com/1004.jpg | | 5 | 22000 | App\User | https://example.com/1005.jpg | | 6 | 22002 | App\User | https://example.com/1006.jpg | | 7 | 4 | App\Post | https://example.com/1007.jpg | | 8 | 5 | App\Post | https://example.com/1008.jpg | | 9 | 22003 | App\User | https://example.com/1009.jpg | | 10 | 22004 | App\User | https://example.com/1010.jpg | +----+-------------+-----------------+------------------------------+
This is Laravel’s default behavior, but it’s not a good practice to store FQCNs in your database. Tying the data in your database to the particular class name is very brittle and can lead to unforeseen breakages if you ever refactor your classes.
To prevent this, Laravel gives us a way to control what values end up in the database with the Relation::morphMap
method. Using this method, you can give every morphed class a unique key that never changes, even if the class name does change:
use Illuminate\Database\Eloquent\Relations; public function boot() { Relation::morphMap([ 'user' => \App\User::class, 'post' => \App\Post::class, ]); }
Now we’ve broken the association between our class name and the data stored in the database. Instead of seeing \App\User
in the database, we’ll see user
. A good start!
We’re still exposed to one potential problem, though: this mapping is not required. We could create a new Comment
model and forget to add it to the morphMap
, and Laravel will default to the FQCN, leaving us with a bit of a mess.
mysql> select * from images; +----+-------------+-----------------+------------------------------+ | id | imageable_id | imageable_type | url | +----+-------------+-----------------+------------------------------+ | 1 | 1 | post | https://example.com/1001.jpg | | 2 | 2 | post | https://example.com/1002.jpg | | .. | ... | .... | . . . . . . . . . . . . . . | | 10 | 22004 | user | https://example.com/1010.jpg | | 11 | 10 | App\Comment | https://example.com/1011.jpg | | 12 | 11 | App\Comment | https://example.com/1012.jpg | | 13 | 12 | App\Comment | https://example.com/1013.jpg | +----+-------------+-----------------+------------------------------+
Some of our imageable_type
values are correctly decoupled, but because we forgot to map the App\Comment
model to a key, the FQCN still ends up in the database!
Laravel has our back (again) by providing us a method to enforce that every morphed model is mapped. You can change your morphMap
call to an enforceMorphMap
call, and the fall-through-to-FQCN behavior is disabled.
use Illuminate\Database\Eloquent\Relations; public function boot() { // Enforce a morph map instead of making it optional. Relation::enforceMorphMap([ 'user' => \App\User::class, 'post' => \App\Post::class, ]); }
Now, if you try to use a new morph that you haven’t mapped, you’ll be greeted with a ClassMorphViolationException
, which you can fix before the bad data makes it to the database.
The most pernicious failures are the silent ones; it’s always better to have explicit failures!
Preventing stray HTTP requests
While testing your application, it’s common to fake outgoing requests to third parties so you can control the various testing scenarios and not spam your providers.
Laravel has offered us a way to do that for a long time by calling Http::fake()
, which fakes all outgoing HTTP requests. Most often, though, you want to fake a specific request and provide a response:
use Illuminate\Support\Facades\Http; // Fake GitHub requests only. Http::fake([ 'github.com/*' => Http::response(['user_id' => '1234'], 200) ]);
In this scenario, outgoing HTTP requests to any other domain will not be faked and will be sent out as regular HTTP requests. You may not notice this until you realize that specific tests are slow or you start hitting rate limits.
Laravel 9.12.0 introduced the preventStrayRequests
method to protect you from making errant requests.
use Illuminate\Support\Facades\Http; // Don’t let any requests go out. Http::preventStrayRequests(); // Fake GitHub requests only. Http::fake([ 'github.com/*' => Http::response(['user_id' => '1234'], 200) ]); // Not faked, so an exception is thrown. Http::get('https://planetscale.com');
This is another good protection to always enable. If your tests need to reach external services, you should explicitly allow that. If you have a base test class, I recommend putting it in the setUp
method of that base class:
protected function setUp(): void { parent::setUp(); Http::preventStrayRequests(); }
In any tests where you need to allow non-mocked requests to go out, you can re-enable that by calling Http::allowStrayRequests()
in that particular test.
Long-running event monitoring
These last few methods aren’t about preventing discrete, incorrect behaviors but rather monitoring the entire application. These methods can be helpful if you don’t have an application performance monitoring tool.
Long database queries
Laravel 9.18.0 introduced the DB::whenQueryingForLongerThan()
method, which allows you to run a callback when cumulative runtime across all of your queries exceeds a certain threshold.
use Illuminate\Support\Facades\DB; public function boot() { // Log a warning if we spend more than a total of 2000ms querying. DB::whenQueryingForLongerThan(2000, function (Connection $connection) { Log::warning("Database queries exceeded 2 seconds on {$connection->getName()}"); }); }
If you want to run a callback when a single query takes a long time, you can do that with a DB::listen
callback.
use Illuminate\Support\Facades\DB; public function boot() { // Log a warning if we spend more than 1000ms on a single query. DB::listen(function ($query) { if ($query->time > 1000) { Log::warning("An individual database query exceeded 1 second.", [ 'sql' => $query->sql ]); } }); }
Again, these are helpful methods if you do not have an APM tool or a query monitoring tool like PlanetScale’s Query Insights.
Request and command lifecycle
Similar to long-running query monitoring, you can monitor when your request or command lifecycle takes longer than a certain threshold. Both of these methods are available beginning with Laravel 9.31.0.
use Illuminate\Contracts\Http\Kernel as HttpKernel; use Illuminate\Contracts\Console\Kernel as ConsoleKernel; public function boot() { if ($this->app->runningInConsole()) { // Log slow commands. $this->app[ConsoleKernel::class]->whenCommandLifecycleIsLongerThan( 5000, function ($startedAt, $input, $status) { Log::warning("A command took longer than 5 seconds."); } ); } else { // Log slow requests. $this->app[HttpKernel::class]->whenRequestLifecycleIsLongerThan( 5000, function ($startedAt, $request, $response) { Log::warning("A request took longer than 5 seconds."); } ); } }
Make the implicit explicit
Many of these Laravel safety features take implicit behaviors and turn them into explicit exceptions. In the early days of a project, it’s easy to keep all of the implicit behaviors in your head, but as time goes on, it’s easy to forget one or two of them and end up in a situation where your application is not behaving as you’d expect.
You have enough things to worry about. Take some off your plate by enabling these protections!