Laravel: What is the purpose of the `loadMissing` function?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel: What is the Purpose of the `loadMissing` Function? Unpacking Eloquent Loading Strategies When working with Eloquent relationships in Laravel, developers frequently encounter discussions about "lazy loading" versus "eager loading." Understanding how data is fetched is fundamental to writing performant and maintainable code. One function often mentioned in this context is `$model->loadMissing()`. While it might seem redundant when dealing with standard property access, its purpose goes deeper than just triggering a load—it’s about explicit control over the data lifecycle. This post will dive deep into what `loadMissing()` actually does, why we use it, and how it fits into a larger strategy for optimizing database interactions, drawing parallels to best practices in Laravel development. ## The Nature of Lazy Loading in Eloquent As noted in the documentation, when you access an Eloquent relationship as a property (e.g., `$book->author`), the data is "lazy loaded." This means that the related data is *not* fetched from the database until that specific property is accessed for the first time. This mechanism saves initial query time if you only need the primary model data, which is a core concept in optimizing database calls within Laravel. The crucial point here is that the relationship exists as a property, but it does not exist as loaded data in memory yet. ## The Role of `loadMissing()`: Explicit Control Over Loading If lazy loading works fine, why introduce `$book->loadMissing('author')`? The answer lies in explicit control and predictability, especially when dealing with complex scopes or methods where you need to ensure related data is present immediately for subsequent operations within the same scope. The purpose of `loadMissing()` is to check whether a specified relationship has been loaded onto the model instance. If it hasn't, it triggers the necessary query to load that relationship (using the appropriate eager loading mechanism behind the scenes) and attaches the resulting data to the model. If the relationship *has* already been loaded, the function simply does nothing, preventing unnecessary database queries. ### When Implicit vs. Explicit Loading Matters Consider a scenario where you are building a complex view or service layer: 1. **Implicit Loading (Lazy):** If you rely solely on `$book->author`, the first access triggers a query for the author. This is fine if you only need that one piece of data later in your code. 2. **Explicit Loading (`loadMissing`):** If you know, based on the context of your current method, that you will definitely need the `author` relationship immediately after this call (perhaps to format a display object), calling `$book->loadMissing('author')` forces the loading *at this moment*. This makes the data available instantly, preventing potential cascading lazy loads or ensuring that subsequent code paths don't unexpectedly trigger new database calls when they expect the data to be present. In essence, `loadMissing()` shifts the control mechanism from "load only when accessed" to "ensure this relationship is loaded now, if it isn't already." This provides a predictable loading pattern that is vital for performance tuning in large applications. ## Code Example and Best Practices Let’s revisit your example to illustrate the practical difference: ```php public function format(Book $book) { // Explicitly load the author relationship if it hasn't been loaded yet. $book->loadMissing('author'); return [ 'name' => $book->name, // Now we can confidently access 'author' without triggering a new query 'author_name' => $book->author->name ?? 'N/A' ]; } ``` In this context, by calling `loadMissing('author')`, we guarantee that `$book->author` exists before we attempt to access its properties. If we had omitted it, the line accessing `$book->author->name` would trigger a separate database query if the relationship hadn't been accessed previously, potentially leading to N+1 query problems if this method were called repeatedly in a loop. For truly massive datasets or complex relationships, always pair `loadMissing()` with proper eager loading when fetching collections: ```php // Best practice for fetching multiple books and their authors efficiently $books = Book::with('author')->get(); // This loads all necessary author data in just two efficient queries, // rather than N+1 queries. See more about eager loading techniques on the // [Laravel documentation](https://laravelcompany.com/docs). ``` ## Conclusion The `loadMissing()` function is not a magic bullet that replaces eager loading; rather, it is a tool for managing the lifecycle of lazy-loaded Eloquent relationships. It provides explicit control, ensuring that related data is present in memory exactly when you need it. By understanding this distinction—the difference between implicit access and explicit loading—you gain finer control over database performance, leading to cleaner, more predictable, and highly optimized Laravel applications.