Get data without appends and relations in Laravel from Eloquent

Get data without appends and relations in Laravel from Eloquent

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Get data without appends and relations in Laravel from Eloquent

When you call Project::find($id), you might expect a simple retrieval of a single record. However, Eloquent is designed to be eager by default when it comes to your defined relationships and accessors. If your Project model has $appends that reference related models like pages, or if those appended attributes trigger further relationship loading, the resulting data structure can become bloated with nested query results you never explicitly asked for.

In the scenario described, accessing $project->pages within a view or controller without using with() triggers a lazy-loading N+1 problem. Even worse, if your $appends logic attempts to resolve relationships internally, the hydration process itself pulls in foreign data. The goal here is strictly limiting the output to the raw Project entity: no relation data, no appended attributes, and absolutely no child records.

To achieve this while staying within Eloquent's syntax, you need to bypass the default accessor resolution chain that triggers relationship fetching. The most direct method involves accessing the underlying model instance directly without invoking the magic getter methods that resolve appends. However, simply casting the result to an array often preserves appended attributes because they are resolved during the toArray process.

You can force a raw extraction by utilizing the model property or accessing the database row as an array before Eloquent resolves any accessors. One effective pattern is to use forceFill combined with specific attribute selection, though this still risks triggering getters if you iterate over the attributes directly in your view. A more robust approach for "no relations, no appends" involves leveraging the attributes property or converting the model to a raw array while disabling the default accessor resolution.

Consider the following implementation strategy:

$project = Project::find($id);

// Access only the base attributes without triggering accessors or appending
$rawData = $project->getAttributes(); // Returns an array of column values only

// Alternatively, using toArray with a custom flag if available in your version
// Note: Standard toArray() includes appends. You must explicitly avoid calling it.

The critical distinction lies in how Laravel handles the model's state. When you call $project->toArray(), it merges the attributes array, the relations array, and the appends array into a single flat structure. This is why your view renders pages even if you didn't ask for them. By accessing $project->getAttributes() (available in newer Laravel versions) or manually iterating over $project->attributes, you bypass the accessor logic entirely.

If you are using an older Laravel version where getAttributes is not present, you can access the underlying Carbon instance's attributes or cast the model to an array carefully. However, be aware that json_encode($project) will still resolve appends because it internally calls toArray(). To prevent this, you must construct your data structure manually:

$project = Project::find($id);

// Manually extract only the columns from the database row
$data = [
    'id' => $project->id,
    'name' => $project->name,
    // Add other base columns explicitly needed
];

// Do NOT include relations or appended properties here

This manual construction ensures that no relationship data is fetched from the database, keeping your query count at exactly one. It also guarantees that any complex logic defined in your $appends closure is never executed. This approach is particularly useful when rendering API responses where you want to expose a lean dataset without bloating the payload with nested objects or calculated fields that depend on relationships.

The trade-off here is increased boilerplate code for models with many columns. You lose the convenience of toArray(), which automatically includes timestamps, casts, and appends. If your application relies heavily on $appends for presentation logic, this approach might seem restrictive. However, in cases where you need a strictly decoupled data object—for instance, passing JSON to a third-party service or caching only the core identifier—you have no choice but to strip these layers manually.

Remember that accessing any property defined as an accessor (e.g., $project->slug) will invoke the corresponding getter method. If that getter logic references relations or appends, you must avoid calling those properties directly in your data extraction block. Stick strictly to the attributes array or individual column accessors that do not depend on other model state.

Our network