Eager loading is a technique used to load related data along with the primary resource in a single query or API call, reducing the need for subsequent, separate requests to fetch related data. This can improve performance and reduce the number of queries made to the API.In the API, eager loading is facilitated by the with
parameter. For example, when fetching a user (either via an index or a show request), you can include related data, such as the user's company
, by passing company
to the with
parameter. This ensures the user's company is loaded alongside the user resource, eliminating the need for additional API calls to retrieve that relationship.Example#
1.
First, fetch the user (GET /api/user/1
).
Then, fetch the user's company (GET /api/company/42
).
2.
Fetch the user and their company in one request:
GET /api/user/1?with=company
This approach reduces network overhead and latency while making our API more efficient and easier to use for clients that require related data.Multiple Relations#
The with
query parameter accepts arrays and comma-separated keys. The two formats look like this:It is also possible to use the dot notation to fetch nested relations, like company.subscription
. This will result in the subscription being loaded inside the company. Modified at 2025-05-13 05:26:22