Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you explain the difference between loadMissing and loadAll in Pennant


Can you explain the difference between loadMissing and loadAll in Pennant


In Laravel Pennant, `loadMissing` and `loadAll` are methods used for eager loading feature flag values, but they serve different purposes:

- `loadMissing` loads feature values only if they have not already been loaded for the given scopes or users. It checks the in-memory cache and avoids reloading features that are already present, preventing redundant database queries or driver calls. This is useful when you want to ensure that only missing feature flags are fetched, optimizing performance by not repeating queries for features already loaded earlier in the request lifecycle.

- `loadAll` loads all defined feature flags for the given scopes or users, regardless of whether some features have already been loaded. This method ensures that every feature flag defined in the system is eagerly loaded into memory for the specified scopes, which can be helpful when you want to guarantee that all feature flags are available without checking individually.

Context and Use Cases

Imagine you are checking feature flags in a loop for multiple users. Without eager loading, each check could trigger a database query, causing an N+1 query problem. Using Pennant's eager loading methods helps avoid this:

- Using `load` or `loadAll` before the loop fetches all relevant feature flags upfront, reducing queries.
- Using `loadMissing` is more selective; it only loads those features not already cached, avoiding duplicate queries if some features were loaded earlier.

Example from Pennant documentation

php
// Load specific features for users, only if not already loaded
Feature::for($users)->loadMissing([
    'new-api',
    'purchase-button',
    'notifications-beta',
]);

// Load all defined features for users, regardless of cache
Feature::for($users)->loadAll();

Summary

- `loadMissing`: Loads only features not already loaded, preventing duplicate queries and optimizing performance when partial feature data may already be cached.
- `loadAll`: Loads all defined features for the scope, ensuring complete feature flag data is available.

Both methods help mitigate performance issues related to multiple queries in loops by eager loading feature flags, but `loadMissing` is more conservative and avoids reloading features unnecessarily, while `loadAll` is comprehensive and loads everything at once[1][6].

Citations:
[1] https://laravel.com/docs/11.x/pennant
[2] https://stackoverflow.com/questions/47238937/laravel-what-is-the-purpose-of-the-loadmissing-function
[3] https://laravel.com/docs/11.x/eloquent-relationships
[4] https://www.reddit.com/r/laravel/comments/krsefm/question_about_with_vs_load_in_eloquent_models/
[5] https://readouble.com/laravel/11.x/ja/eloquent-relationships.html
[6] https://laravel.com/docs/12.x/pennant
[7] https://www.investopedia.com/terms/p/pennant.asp
[8] https://pennant-notebook.github.io