These are the Eloquent patterns I reach for most often. They are not clever. They are the difference between a query that works on seed data and a query that still works when the table has a million rows.

Configure Eloquent strictness

Add these to AppServiceProvider for sane defaults:

Model::preventLazyLoading(! $this->app->isProduction());
  • Lazy loading is prevented, which avoids the N + 1 problem.
  • Laravel errors when a model attempts to lazy load a relationship.
  • You must explicitly eager load with with().
Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());
  • Prevents Laravel from ignoring attributes that do not exist on the model’s $fillable list or database table.
  • Errors if you try to set or retrieve an attribute that is not actually defined for that model.
  • Catches typos, outdated column names, and incorrect mass-assignment keys during development.

Retrieve models in chunks

Most of the time, use lazy() for chunking and memory-efficient per-model access.

use App\Models\Flight;

foreach (Flight::lazy() as $flight) {
    // ...
}
use App\Models\Flight;

Flight::where('departed', true)
    ->lazyById(200, column: 'id')
    ->each->update(['departed' => false]);

Prefer observers for model events

Use an observer for the actions that should happen around a model event.

use App\Observers\UserObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;

#[ObservedBy([UserObserver::class])]
class User extends Authenticatable
{
    //
}

Has many: use chaperone()

Always use chaperone() on a has-many relationship to avoid a sneaky N + 1.

class Post extends Model
{
    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class)->chaperone();
    }
}

Even with eager loading, accessing the parent from each child (for example, $comment->post) can trigger an additional query per child. chaperone() hydrates the parent automatically.

Belongs to: use whereBelongsTo()

Use whereBelongsTo() to get the children of a parent or a collection of parents.

// All posts that belong to $user
$posts = Post::whereBelongsTo($user)->get();

// All posts that belong to VIP users
$users = User::where('vip', true)->get();
$posts = Post::whereBelongsTo($users)->get();

Has one of many

If you have ever written “get the latest X per Y,” latestOfMany() is the relationship you want. It eager loads efficiently instead of looping.

class Customer extends Model
{
    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    public function latestOrder(): HasOne
    {
        return $this->orders()->one()->latestOfMany();

        // If orders() is not already declared:
        // return $this->hasOne(Order::class)->latestOfMany();
    }
}

$customers = Customer::with('latestOrder')->get();

Behind the scenes, Laravel runs one query for all customers and one query with a join and grouping to fetch each customer’s latest order in bulk. Two queries, even with five thousand customers.

Has one through

When you need one related record through another table for many parents at once, eager loading keeps it at two queries.

class Store extends Model
{
    public function manager(): HasOne
    {
        return $this->hasOne(Manager::class);
    }

    public function managerContact(): HasOneThrough
    {
        return $this->hasOneThrough(
            ContactInfo::class, // Final model
            Manager::class,     // Intermediate model
            'store_id',         // FK on managers table
            'manager_id',       // FK on contact_infos table
            'id',               // Local key on stores table
            'id'                // Local key on managers table
        );

        // Or with the through syntax:
        // return $this->through('manager')->has('contactInfo');
    }
}

$stores = Store::with('managerContact')->get(); // Always two queries

Has many through

The same idea for many related records through another table.

class Branch extends Model
{
    public function employees(): HasMany
    {
        return $this->hasMany(Employee::class);
    }

    public function sales(): HasManyThrough
    {
        return $this->hasManyThrough(
            Sale::class,      // Final model
            Employee::class,  // Intermediate model
            'branch_id',      // FK on employees table
            'employee_id',    // FK on sales table
            'id',             // Local key on branches table
            'id'              // Local key on employees table
        );

        // Or with the through syntax:
        // return $this->through('employees')->has('sales');
    }
}

$branches = Branch::with('sales')->get(); // Always two queries

The pattern

Strictness in development. Explicitness in queries. Relationships that load in two queries instead of two hundred. None of it is glamorous. All of it compounds.

Thanks for reading.

Written by Jim Merioles / August 13, 2025