Optimizing Laravel Performance: Tips for Faster APIs

1. Cache Your Routes for Faster Request Handling

What It Means:

When Laravel processes a request, it goes through all the route files to find a match. This can take a bit of time, especially as your number of routes increases.

The Solution:

Utilize route caching to compile all your routes into a single file.

How to Do It:

php artisan route:cache

This command will generate a cached routes file, which Laravel can read much faster.

To Clear the Cache:

php artisan route:clear

Tip: Only use this in production. During development, cached routes may hide updates.


2. Optimize Your Database Queries

Problem:

Most slow APIs happen because of inefficient or unnecessary database queries.

Best Practices:

a. Use Eager Loading to Avoid N+1 Queries

Bad Example (Lazy Loading):

$users = User::all();

foreach ($users as $user) {
    echo $user->posts->count(); // Triggers a new query each time
}

Better Example (Eager Loading):

$users = User::with('posts')->get();

foreach ($users as $user) {
    echo $user->posts->count(); // already-loaded posts data
}

This reduces the number of queries and boosts performance significantly.


b. Avoid Unnecessary Queries

Instead of this:

$user = User::where('email', 'test@example.com')->first();

Do this:

$user = User::firstWhere('email', 'test@example.com');

Both do the same thing, but the second one is shorter and cleaner.


c. Use select() to Limit Returned Columns

If you don’t need all columns, don’t fetch them:

$users = User::select('id', 'name')->get();

This reduces payload size and improves query speed.


3. Remove Unused Services and Middleware

Why It Matters:

Each request runs through all registered middleware. Extra or unused middleware slows down your app.

What You Can Do:

  • Review app/Http/Kernel.php and remove any middleware you don’t need.

  • Disable unnecessary service providers in config/app.php.


4. Use Laravel’s Caching System Wisely

Laravel supports caching via file, Redis, Memcached, and database drivers.

a. Cache Configuration Files

php artisan config:cache

Laravel combines all config files into one file for faster loading.


b. Cache Views

php artisan view:cache

This compiles Blade templates into PHP and speeds up rendering.


c. Cache Expensive Queries

$posts = Cache::remember('popular_posts', now()->addMinutes(10), function () {
    return Post::orderBy('views', 'desc')->take(5)->get();
});

This avoids running the same query over and over again.


5. Use Queues for Time-Consuming Tasks

Why:

If you’re doing things like sending emails, uploading files, or generating PDFs directly in your API, your response will be slow.

Use Laravel Queues:

Step 1: Create a Job

php artisan make:job SendWelcomeEmail

Step 2: Dispatch the Job

SendWelcomeEmail::dispatch($user);

Step 3: Run the Queue Worker

php artisan queue:work

This sends the email in the background, while your API returns a fast response.


6. Paginate Large Datasets

Bad: Returning Too Much Data

return User::all(); // May return thousands of records

Good: Use Pagination

return User::paginate(20); // Returns 20 records per page

You can also use:

return User::simplePaginate(20); // Lighter version without total page count

7. Clean and Minimize JSON Responses (with Laravel API Resources)

When building APIs, returning the entire model data is not a good idea, it exposes unnecessary fields (like passwords, timestamps, etc.) and makes your response payload heavier.

The Solution: Use Laravel’s API Resources

Laravel provides Resource Classes to control how your models are converted to JSON responses. You get cleaner, lighter, and safer outputs.

Step-by-Step: How to Create and Use Laravel API Resources


Step 1: Generate a Resource Class

Run this Artisan command to generate a resource:

php artisan make:resource UserResource

This will create a file:

app/Http/Resources/UserResource.php

Step 2: Define the Data You Want to Return

Open UserResource.php and customize the toArray() method:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'    => $this->id,
            'name'  => $this->name,
            'email' => $this->email,
            'joined' => $this->created_at->format('Y-m-d'),
        ];
    }
}

Only the selected fields will be returned in the API response.


Step 3: Use the Resource in Your Controller

Instead of returning the full model:

<?php

namespace App\Http\Controllers;

class UserController extends Controller 
{
    public function show($id)
    {
        return User::find($id);
    }
}

Use the resource:

<?php

namespace App\Http\Controllers;

use App\Http\Resources\UserResource;

class UserController extends Controller 
{
    public function show($id)
    {
        return response()->json([
                'success' => true,
                'message' => 'User data successfully fetched',
                'data' =>  UserResource::make(User::find($id)),
            ], 200);    
    }
}

For Collections (Multiple Users):

If you’re returning a list of users, use:

UserResource::collection(User::all())

Make sure you eager load the relation in the controller:

$user = User::with('posts')->find($id);
return new UserResource($user);

Avoid This:

return response()->json($user); // Dumps the whole model, including sensitive data

Instead, always use a Resource class to protect and control your API output.


💡 Tip: Hide Attributes with $this->mergeWhen

Return values only when certain conditions are met:

return [
    'id' => $this->id,
    'email' => $this->when(auth()->user()->isAdmin(), $this->email),
];

8. Use Redis for Cache and Queues

Redis is fast and efficient for handling cache, session, and queue data.

Install Redis and Update .env

CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis

Install the PHP Redis extension via:

sudo apt install php-redis

Or using Composer:

composer require predis/predis

9. Use Debug Tools in Development

Use Laravel Debugbar

composer require barryvdh/laravel-debugbar --dev

This shows you:

  • Total execution time

  • Number of queries

  • Memory usage

  • Request details

Use Laravel Telescope

composer require laravel/telescope --dev
php artisan telescope:install
php artisan migrate

Telescope is like a mini control panel to inspect requests, jobs, DB queries, and more.

❗ Don’t use these in production, they can slow things down.


Final Thoughts

You don’t need to be an expert to start optimizing your Laravel APIs. Just take small steps:

  • Cache where possible

  • Query smartly

  • Avoid doing too much in one request

Over time, these best practices will make your app faster, more scalable, and more professional.

Conclusion

Optimizing Laravel API performance does not have to be complex. it is about consistently applying the appropriate practices.

By caching routes, reducing database load, minimizing JSON responses, and offloading heavy tasks to queues, you can greatly enhance the speed, scalability, and reliability of your Laravel APIs.

Leveraging Laravel’s powerful tools like Eager Loading, API Resources, and built-in caching gives you detailed control over your app’s performance without making things too complex.

Start small, focus on optimizing one part at a time, and soon your APIs will be faster, cleaner, and ready to handle production-level traffic. 🚀

2
Subscribe to my newsletter

Read articles from Anik Kumar Nandi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anik Kumar Nandi
Anik Kumar Nandi

Hi there! I'm Anik Kumar Nandi, passionate programmer, strongly focus on backend technologies. I specialize in PHP and Laravel, and I'm always exploring new ways to enhance the robustness and efficiency of my code. In addition to PHP and Laravel, my skill set includes RESTful API development, JavaScript, jQuery, Bootstrap, Ajax, and WordPress customization. Also deeply interested in AWS. Feel free to check out my GitHub profile and my portfolio site to learn more about my work and projects. Let's connect and create something amazing together!