Laravel offers two popular ways to pass data to your Blade templates: with() and PHP’s own compact(). Both get the job done, but each shapes how your code feels to read and maintain, especially in larger projects where clarity really matters.
Think of it like choosing between writing out instructions clearly or using a quick shorthand. Both approaches work, but each fits a different scenario.
Using with() to Pass Data Clearly
Using with() feels like speaking clearly to your future self or your teammates. You’re not just sending data — you’re explaining what each piece means in the view:
public function showUsers(){ $users = User::all();
return view('users.index')->with('users', $users);
}
Straightforward and descriptive. And if you need to send more data, you can chain:
Need to send multiple items? You can chain with() like this:
return view('dashboard') ->with('users', $users)
->with('stats', $stats);
The real value? Flexibility. Let’s say your controller uses $activeUsers but the view should just see $users. With with(), you decide the final key name. That keeps your Blade files cleaner and clearer, especially when data names get complicated.
In bigger teams, this explicit style helps new developers understand what data each view expects without guessing.
Using compact() for Cleaner Syntax
The cool part is compact() isn’t even Laravel-specific — it’s been around in PHP forever. Instead of writing out an array, you pass variable names as strings, and PHP builds the array automatically:
public function showUsers(){
$title = 'User List';
$users = User::all();
return view('users.index', compact('title', 'users'));
}
Behind the scenes, it becomes ['title' => $title, 'users' => $users].
It shines when your variable names already match what your view expects. You save a few lines and keep your controller method short, which can look cleaner:
return view('dashboard', compact('title', 'users', 'stats'));
One small caveat: if your variable names aren’t clear (like $info or $data), the view ends up just as unclear. compact() doesn’t add context it just bundles.
Worth mentioning:
According to Stack Overflow’s 2024 survey, PHP remains the go-to choice for 18.2% of developers.
Deciding between with() and compact()
From experience, here’s when each makes sense:
- Go with with() when you want to rename data or keep view keys extra descriptive.
- Use compact() when your variable names already fit what the Blade template needs.
Neither breaks Laravel best practices, but clarity always wins, especially on teams. Code isn’t just for computers it’s for people who’ll maintain it next month.
📚 Also worth reading:
Common Mistakes & Tips
Even experienced developers can slip up:
- Passing a variable to compact() before defining it instant PHP error.
- Using the same key twice with with(), accidentally overwriting earlier data.
- Mixing with() and compact() together, which technically works but makes controllers harder to read.
- Changing variable names in Blade views without updating the controller, leading to undefined errors.
Quick fix: before calling return view(), double-check what’s really defined and what the view expects.
Also Read: The Key Features of Laravel 7 Framework
Quick Recap
- with(): Pick custom, descriptive view keys; helps when data needs renaming.
- compact(): Faster and cleaner when your variable names already match view keys.
- Both methods work perfectly in Laravel 12 (2025) and will likely stay relevant in future versions.
Conclusion
There isn’t really a “wrong” choice here; it’s about clarity. Use with() when you need custom key names, and reach for compact() when your controller variables already match what the view expects.
Knowing both keeps your Laravel projects tidy and your code easier to work with, whether it’s just you or a whole team coming back to it later.