Laravel - Model Eloquent without relationships

Laravel - Model Eloquent without relationships

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Cost of Event::all() in Laravel 8

A common scenario arises when you define several relationships on a model, such as multiple hasMany associations and a hasOne, yet you only intend to fetch the primary records. You run $events = Event::all(), expecting to retrieve just the event columns. Instead, your database query log reveals a cascade of additional queries hitting the related tables for every single event fetched.

This behavior occurs because Eloquent models are eager by default when using the all() method in certain contexts, or more accurately, they load relationships lazily only if you access them. However, the specific issue described—where relationships appear loaded immediately upon calling Event::all() without an explicit $with array—is a classic case of eager loading via implicit eager loading or simply accessing properties that trigger getters which in turn trigger the relationship queries.

Wait, let's clarify the mechanics here. In standard Eloquent usage, calling Model::all() returns a collection where relationships are not loaded by default. If you see extra queries immediately after $events = Event::all(), it is highly likely that you are accessing a property or method on those models within the same scope or immediately following the call, triggering the lazy loading mechanism.

However, there is a specific nuance in Laravel 8 regarding how all() interacts with eager loading hints. If your code looks like this:

$events = Event::all(); // No $with defined
// ...
foreach ($events as $event) {
    echo count($event->items); 
}

The first access to $event->items triggers a query for that specific relationship. But if you are seeing queries fired immediately upon the Event::all() call without touching any properties, consider this: did you accidentally pass a $with array in your controller or service layer? Or, perhaps more subtly, are you using a custom accessor that accesses the relationships internally?

If you strictly want to avoid these N+1 style queries or unintended relationship loads when retrieving a bulk list, you must explicitly disable eager loading. You can do this by passing an empty array to the with method, effectively telling Eloquent "load nothing extra":

$events = Event::all(['*']); // This doesn't help for relationships
// Correct approach:
$events = Event::all(); 
// To be absolutely sure no relationships are touched before you access them:
// You must not access any relationship property until you are ready.

Actually, the most direct way to ensure zero relationship loading is to avoid accessing any relationship properties on the models immediately after fetching them. If your code structure inadvertently accesses $event->items inside a loop right after Event::all(), Eloquent will load that relationship for every row.

To prevent this entirely, you can explicitly tell Eloquent not to eager load anything by using an empty array in the query:

$events = Event::with([])->get();

This is the robust solution. Even if your model class defines items(), items2(), and items4() methods, passing an empty array ensures Eloquent does not generate any JOINs or subqueries to populate those attributes. This keeps your query lightweight and focused solely on the events table.

Why does this matter? Performance is paramount when scaling applications. Unintended relationship loading can silently degrade performance as your dataset grows. The Event::all() method itself doesn't inherently load relationships unless you specify them via $with or access them lazily. If your logs show unexpected queries, inspect the code block immediately following the fetch operation.

For further reading on optimizing query performance in Laravel 8 and later, you might want to check out resources like https://laravelcompany.com which often discusses best practices for handling model relationships and query optimization. Understanding these nuances ensures your application remains efficient even as data volume increases.

Our network