Skip to main content

20 Laravel Tips Every Developer Should Know (But Commonly Overlooked)

20 Laravel Tips Every Developer Should Know (But Commonly Overlooked)

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

Popular posts from this blog

How to Display Flash Messages in EJS using Node.js and Express

Displaying Flash Messages in EJS with Node.js and Express Flash messages are a great way to give users quick feedback — like "Login successful!" or "Please enter all fields!" . In this guide, you’ll learn how to implement them using: express-session connect-flash EJS templating ๐Ÿ“ฆ Step 1: Install Required Packages npm install express express-session connect-flash ejs ⚙️ Step 2: Setup Express App and Middleware const express = require('express'); const session = require('express-session'); const flash = require('connect-flash'); const app = express(); // Set view engine app.set('view engine', 'ejs'); // Middleware app.use(express.urlencoded({ extended: true })); app.use(session({ secret: 'yourSecretKey', resave: false, saveUninitialized: true })); app.use(flash()); // Make flash messages available to all views app.use((req, res, next) => { res.lo...

Realtime Device Tracker using Node.js, Socket.IO & Leaflet

Realtime Device Tracker using Node.js, Socket.IO & Leaflet In this tutorial, you’ll learn how to build a realtime location tracking application that uses the browser’s GPS, Socket.IO for live communication, and Leaflet.js to display users on a map. ๐Ÿš€ Project Overview Backend: Node.js, Express.js, Socket.IO Frontend: HTML, Leaflet.js, Socket.IO client Features: Live GPS tracking, multi-user map, disconnect cleanup ๐Ÿ“ Folder Structure project-root/ ├── app.js ├── package.json ├── src/ │ ├── public/ │ │ ├── css/ │ │ │ └── style.css │ │ └── js/ │ │ └── script.js │ ├── routes/ │ │ └── routers.js │ ├── socket/ │ │ └── socketHandler.js │ └── views/ │ └── index.ejs ๐Ÿง  How It Works Each user shares their location using the browser's navigator.geolocation API. Location is sent to the server via Socket.IO . The server broadcasts each user’s position to all clien...

How to Send Emails in Node.js using Nodemailer and Ethereal

How to Send Email in Node.js using Nodemailer Email functionality is essential in modern web applications. Whether you're sending confirmation emails, password resets, or notifications, Node.js with Nodemailer makes this simple. In this blog, we'll walk through setting up email sending using Node.js , Express , and Ethereal Email for testing. ๐Ÿงพ Prerequisites Node.js installed Basic knowledge of Express.js Internet connection ๐Ÿ“ Project Structure project-folder/ ├── index.js ├── .env ├── package.json └── app/ └── controller/ └── emailSendController.js ๐Ÿ“ฆ Step 1: Install Dependencies npm init -y npm install express nodemailer dotenv npm install --save-dev nodemon ๐Ÿ” Configure nodemon (Optional but Recommended) Update your package.json with a custom start script: "scripts": { "start": "nodemon index.js" } ๐Ÿ” Step 2: Create a .env File Create a .env file a...