Laravel is powerful, elegant, and developer-friendly—but many of its gems remain hidden or underused. Here are 20 Laravel tips and features that you might not be using yet but definitely should.
1. Eloquent Accessors & Mutators
Format your model's attributes when retrieving (Accessor) or setting (Mutator) them.
public function getNameAttribute($value) {
return ucfirst($value);
}
2. Route Model Binding
Let Laravel automatically inject models into routes, making code cleaner and error-free.
Route::get('/user/{user}', function(User $user) {
return $user;
});
3. $fillable and $guarded
Control which fields can be mass assigned to prevent mass assignment vulnerabilities.
4. Query Builder’s when() Method
Build clean conditional queries without unnecessary if-else blocks.
5. Blade Directives: @isset, @empty, @auth
Make your Blade templates cleaner and more readable using built-in directives.
6. pluck() Method
Quickly extract single columns from collections or query results.
User::pluck('email');
7. Blade Components
Reuse layout elements like alerts, buttons, or modals using Blade components.
8. Custom Validation Rules
Encapsulate reusable validation logic using Laravel's custom rule classes.
9. tap() Helper Function
Modify and return a value in the middle of a method chain—great for debugging or logging.
10. Form Requests
Move validation logic into dedicated request classes to keep controllers slim and focused.
11. updateOrCreate() and firstOrCreate()
Avoid repetitive if-else checks when updating or inserting records.
12. withCount() on Relationships
Easily retrieve count of related models (e.g., comments count on a post) in a single query.
13. withDefault() on Relationships
Set default values when a related model doesn’t exist—no more null errors.
$user->profile->name // returns default if no profile
14. Local Query Scopes
Encapsulate common conditions into reusable model scopes.
public function scopeActive($query) {
return $query->where('status', 'active');
}
15. Task Scheduling
Automate routine tasks (like sending emails or database cleanup) without using external cron scripts.
16. Job Batching
Queue and track multiple jobs as a single unit using Laravel's batch processing.
17. Laravel Collections
Use map, filter, reduce, and other higher-order methods to process arrays in a clean, functional style.
18. Artisan Tinker
Play with your application in real time using Laravel's powerful REPL for testing models, DB, and more.
19. Laravel Debugbar
Install barryvdh/laravel-debugbar
to debug queries, views, and performance in development mode.
20. Macros for Extending Core Classes
Add custom reusable methods to Laravel's core classes (Collection, Response, Request, etc.).
Final Thoughts
These lesser-known Laravel features can save you time, reduce bugs, and improve your codebase quality. Try using a few of them in your next project and see the difference yourself.
Liked this article? Follow me on The Learn with Rana for more content on Laravel, JavaScript, and full-stack development. Also connect with me on LinkedIn for regular developer tips!
Comments
Post a Comment