Need to pull related data in Laravel without extra queries? The with and find () method lets you grab connected records in one go fast, clean, and perfect when dealing with parent-child relationships like authors and their articles or users and their comments.
It’s the difference between one smart call vs. dozens of slow ones. If you’re tired of chasing down nested data the hard way, this approach cuts through the noise and speeds things up without overthinking the logic.
Stick around we’re unpacking smarter ways to fetch, filter and group related data that feel like second nature.
Using with() in Laravel to Load Related Data
Need to fetch users along with their connected data in one clean hit? with() makes it possible no loops, no extra queries.
That single line loads all users and their profile info in one go instead of querying each profile separately. Want more than one relation?
You can even keep things readable while loading multiple layers of connected data. It’s built for clarity, not chaos.
Now, say you want to filter that nested relation like only grabbing recent comments per user. Here’s how:
$users = UserC::with([‘comments’ => function ($q) { $q->latest()->limit(3);}])->get();
Need to go deeper with grouped conditions? Laravel’s still got your back:
$q->orderBy(‘created_at’, ‘desc’);}])->whereHas(‘comments’, function ($q) { $q->havingRaw(‘COUNT(*) > 5’);})->get();
Also Read: Laravel Eloquent – Understanding Has, With, and WhereHas
When to Use find() in Laravel
The find() method retrieves a record by its primary key. It’s the cleanest way to get a single entry or a group when you already know their IDs.
Example:
$a = User::find(1); // Get user with ID 1
$b = User::find([2, 3, 4]); // Get users with IDs 2, 3, and 4
It also works inside relationships:
Did You Know?
Laravel runs behind more than 680,000 live websites today. In the U.S. alone, over 270,000 websites rely on it for backend work. And historically? Laravel has powered close to 2 million sites worldwide that’s a legacy not many frameworks can claim.
Smarter Queries with with() + Conditions
You can pass closures into with() to fine-tune how related data gets loaded — like sorting or filtering it on the fly.
Example:
$users = User::with([‘comments’ => function ($q) { $q->latest();}])->whereHas(‘comments’, function ($q) {
$q->where(‘status’, ‘approved’);
})->get();
In this case, it loads only users who have approved comments and fetches those comments ordered from newest to oldest.
Also Read: Custom Validation Rules in Laravel
Conclusion
Use with() when you need to fetch related data efficiently in a single query. It helps reduce N+1 issues and improves performance for relationship-heavy structures.
Use find() when you’re retrieving specific records by ID. It’s direct, fast, and best used when relationships aren’t involved. Always choose based on the context of what data needs to be fetched.