Laravel makes it easy to build web applications, but as an application grows, you may notice that some pages take longer to load. Slow database queries, unnecessary API calls, and repeated calculations can affect the overall performance.
The good news is that you don’t always need complicated tools or major code changes to improve speed. A few practical improvements can make a noticeable difference.
If you’re using Laravel and want to understand how the framework has evolved, check out our guide to Laravel versions compared.
In this article, we’ll look at five techniques I commonly recommend when working with Laravel applications.
1. Use Eager Loading to Avoid N+1 Queries
One common performance issue in Laravel is the N+1 query problem.
Imagine you have a product listing page that displays each product’s category name. If you access the category inside a loop without eager loading, Laravel may execute an additional database query for every product.
Avoid this:
$products = Product::all();
foreach ($products as $product) {
echo $product->category->name;
}
Use eager loading instead:
$products = Product::with('category')->get();
foreach ($products as $product) {
echo $product->category->name;
}
Laravel loads the related categories more efficiently instead of repeatedly querying the database.
My suggestion: Whenever you display related records in a list, check whether eager loading with with() can reduce unnecessary queries.
2. Use Caching for Frequently Accessed Data
If your application repeatedly fetches the same data, you may be doing unnecessary database work.
For example, website settings, category lists, and frequently used dashboard statistics can often be cached.
use Illuminate\Support\Facades\Cache;
$categories = Cache::remember(
'active_categories',
3600,
function () {
return Category::where('status', 1)->get();
}
);
The result is stored for one hour. Subsequent requests can retrieve it from the cache instead of running the same database query again.
When the category data changes, clear the cache:
Cache::forget('active_categories');
Laravel supports different cache drivers, including file, database, and Redis.
My suggestion: Start by caching data that changes infrequently. Remember to clear or refresh the cache whenever that data is updated.
3. Retrieve Only the Database Columns You Need
A small change in your database queries can reduce unnecessary data transfer. When building Laravel REST APIs, returning only the required data can also help reduce response size and improve API efficiency. Read our guide to Laravel REST API development to learn more.
For example, if you only need a product’s name and price, there is no reason to retrieve every column from the products table.
Instead of:
$products = Product::all();
Use:
$products = Product::select('id', 'name', 'price')
->get();
This is particularly useful when working with large tables or API responses containing many records.
You can also add database indexes to columns frequently used in search conditions.
For example:
Schema::table('products', function (Blueprint $table) {
$table->index('status');
});
Before adding indexes, check your actual query patterns and use MySQL’s EXPLAIN to understand how queries are executed.
My suggestion: Avoid fetching unnecessary columns, and don’t add indexes blindly. Too many indexes can also slow down database writes.
4. Use Pagination Instead of Loading All Records
Loading thousands of records on a single page is rarely a good idea.
For example, an admin panel displaying 5,000 customer records may become slow if every record is loaded at once.
Instead, use Laravel pagination:
$customers = Customer::latest()->paginate(20);
In your Blade file:
@foreach ($customers as $customer)
<h4>{{ $customer->name }}</h4>
@endforeach
{{ $customers->links() }}
Now Laravel retrieves only the records needed for the current page.
For very large datasets with sequential navigation, cursorPaginate() can also be useful.
My suggestion: Use pagination for customer lists, orders, products, reports, and other data-heavy screens.
5. Optimize Your Application Before Production Deployment
Laravel provides useful Artisan commands to prepare an application for production.
Run:
php artisan optimize
This command prepares commonly used framework caches, including configuration, events, routes, and views.
If you need to clear these optimization caches, use:
php artisan optimize:clear
You should also make sure PHP OPcache is enabled on your production server. It helps avoid repeatedly compiling PHP scripts.
One important point: don’t blindly run production optimization commands in your local development environment. Configuration changes are easier to work with when you are developing.
My suggestion: Include production optimization in your deployment checklist and test the application after deployment.
Hello, Welcome to webdeveloperindia.in. I am a full-stack web developer. Email – [email protected] | Skype – azaruddin23. I have knowledge of PHP, Laravel, Magento 1/2, Codeigniter, WordPress, Joomla, Shopify, Git, Bitbuket, jQuery, Ajax, Javascript and ReactJS.


