Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any specific libraries or tools that support the whenLoaded() method


Are there any specific libraries or tools that support the whenLoaded() method


The `whenLoaded()` method is specifically supported in the Laravel PHP framework, particularly within its Eloquent ORM and API Resources system. It is used to conditionally include relationship data in API responses only if the relationship has already been loaded on the model, thereby optimizing performance by avoiding unnecessary database queries.

Libraries and Tools Supporting `whenLoaded()`

**Laravel Framework (Eloquent ORM and API Resources):
- The `whenLoaded()` method is a feature of Laravel's Eloquent ORM used in API Resource classes to conditionally include related model data in JSON responses. It checks if a relationship has been loaded on the model and only includes it if so. This helps prevent the N+1 query problem and improves API response efficiency.
- It is used by passing the name of the relationship to `whenLoaded()`, which returns the relationship data if loaded or excludes it otherwise. For example:

php
  public function toArray($request)
  {
      return [
          'id' => $this->id,
          'name' => $this->name,
          'posts' => PostResource::collection($this->whenLoaded('posts')),
      ];
  }
  

- This method is part of Laravel's API Resource classes and is documented in Laravel's official documentation and community tutorials[3][5][6].

**Ruby on Rails (Grape Entity Gem) - Inspired Implementation:
- While `whenLoaded()` is not natively available in Ruby on Rails, a similar conditional loading pattern can be implemented using the `grape` gem's entity exposure with conditional logic.
- In this approach, the exposure of associations like comments is conditioned on whether the association is loaded, mimicking Laravel's `whenLoaded()` behavior:

ruby
  expose :comments,
         using: CommentEntities,
         if: -> (instance, _options) { instance.association(:comments).loaded? }
  

- This allows Rails developers to achieve similar conditional relationship serialization when using Grape entities for APIs[7].

Limitations and Notes

- Laravel's `whenLoaded()` works only with relationships that have been loaded on the model instance. It does not support nested or deeper-than-one-level relations directly, which may require manual checks or alternative approaches[1].
- It is designed for use within Laravel's resource response system and does not exist as a standalone library or in other frameworks outside of Laravel without custom implementation.
- Alternatives or similar patterns in other frameworks usually require manual conditional checks or use of serialization libraries that support conditional exposure of associations.

Summary

- The `whenLoaded()` method is a Laravel-specific feature found in Eloquent API Resources for conditional inclusion of loaded relationships in JSON responses.
- It helps optimize API performance by avoiding unnecessary loading of relationships and database queries.
- A similar concept can be implemented in Ruby on Rails using the Grape gem's conditional exposure feature, but no direct equivalent library outside Laravel exists.
- It is not a general-purpose method but tied closely to Laravel's ORM and resource system.

Thus, if you are working within Laravel, `whenLoaded()` is supported natively in Eloquent API Resources. For other environments, you would need to implement similar conditional logic manually or using framework-specific serialization tools.

Citations:
[1] https://stackoverflow.com/questions/49965081/how-do-i-use-whenloaded-for-deeper-than-one-level-relations
[2] https://remysharp.com/2007/04/12/how-to-detect-when-an-external-library-has-loaded
[3] https://www.interviewsolutionshub.com/blog/laravel-whenloaded
[4] https://rencore.com/en/blog/loading-a-javascript-library-from-a-cdn
[5] https://laravel-news.com/whenloaded
[6] https://laravel.com/docs/12.x/eloquent-resources
[7] https://dev.to/whchi/how-to-implement-laravels-whenloaded-function-in-ror-57hm
[8] https://stackoverflow.com/questions/76692166/what-can-be-used-instead-of-whenloaded-in-resource-when-data-are-set-with-setatt