Optimizing Laravel Performance

April 28, 2016

Laravel is currently the most popular PHP framework for building a variety of sites and applications. Here at Fresh, we’ve built both complex Laravel API platforms and simple portals. The set of libraries and architecture that Laravel provides makes it easy to build amazing sites using well structured and “beautiful code” (their words exactly).

Sometimes with frameworks you wind up sacrificing performance for ease of use. Laravel has done a good job at keeping their framework performant, but here are a few things you can do to help further increase Laravel performance.

Minimize Plugins

There are a wide variety of plugins for Laravel that allow you to easily add more functionality. With that increased functionality come more libraries (and files) to load, which can slow things down. Make sure to take a peek at which providers you are loading through your config/app.php file and trim out unnecessary ones. In addition, Laravel uses composer to manage its components, so pruning your composer.json file will help reduce the dependencies that are loading.

Use “Eager Loading” of Your Data

Laravel uses the Eloquent ORM to easily map your object models to the database tables supporting it. With a few simple files you can map out your object structure, and Eloquent will handle all the appropriate database interaction needed for your CRUD (create, retrieve, update, delete) operations. When Eloquent does it, it uses a “lazy loading” approach. That means for any related data, such as the author details for a book, Eloquent won’t actually retrieve the data until it is specifically referenced somewhere else in the code. While it may seem harmless to have one or two extra queries on your details page, making a similar query against a collection of books can result in a lot of queries and reduced performance as you wait for all of your data to return.

Instead, you want to set up your queries to use “eager loading,” which means they will retrieve any associated object models as part of your initial query. That way they are immediately available for use. Your typical…


$books = App\Book::all();
foreach ($books as $book) {
echo $book->author->name;

…lazy loaded query becomes an…


$books = App\Book::with('author')->get();
foreach ($books as $book) {
echo $book->author->name;

…eager loaded query.

Laravel provides a nice overview on this here.

Trim Your Response Data

This typically applies to API type frameworks, but can also apply to traditional MVC applications created within Laravel. With Eloquent at your fingertips, it is easy to query a comprehensive set of data to be used for display. For example: your book may have a collection of authors, and each of those authors has a basic biography, which also includes a list of events (and their related locations) that they are appearing at over the next three months. When you return a large list of books, regardless of how fast the query completes, passing all of this related data increases the data return size, and causes a delay as the client waits for the data.

If you don’t need all of this data, then you’ve wasted a lot of time (and bandwidth) in your app. The solution to this is to use “View Models,” an optimized subset of data from your data models, containing only the data elements you need for display. If you’re building an API or returning JSON data, you can use a library such as Fractal to easily translate your object model data and include only what you need.

Precompile Assets

For development, having all of your assets in separate files (such as routes and configuration files) is helpful for code maintenance. For production, this isn’t necessary. To help with this, Laravel has a few artisan commands available that you can run before deploying your site:


php artisan optimize
php artisan config:cache
php artisan route:cache

These commands will compile your frequently used classes into a single file for quick reference. They will also combine your configuration files and routes into single files for faster loading. You can also add your own classes into the optimization that might not be added by default. The performance increase will vary, but every little bit helps. More details about these optimizations can be found here and here.

Consider Lumen

Are you building an API or a really simple application that doesn’t require a “full stack” framework? If so, consider Lumen, a micro-framework that has the same elegance in mind as Laravel, just without a lot of additional libraries you may not need. Lumen is stateless, so complicated authentication and session management is replaced to leverage API tokens or headers.


Laravel provides a great framework and good performance out of the box. As your code grows more complex, you may need to optimize a few things for increased performance, and these tricks should help you along the way.

Sean Patterson

Software Development Director

Sean Patterson is a Software Development Director at Fresh Consulting. By day he develops applications at Fresh with the motto "If you can dream it, I can build it." By night he's a java overlord (of the coffee persuasion), aspiring running junkie, spider killer for his wife, and silly daddy to his twin daughters.

Unless otherwise specified, source code in this post is licensed under a
Creative Commons Attribution 4.0 International license (CC BY 4.0).

You might also like...



BalanceTech: A Resource for Finding Balance in a Tech-Reliant World

As technology layers deeper and deeper into the fabric of our everyday life, its impacts on our health must be considered to ensure our long-term well-being. At Fresh, we believe that technology can be a means of enhancing and expanding on our innate human qualities of imagination, innovation, social connection, and productivity. By finding the … Continued



Machine Learning UI Starts with Trust

With the increasing amount of data, the ability to detect patterns and surface them in a trustworthy and actionable manner is key to remaining competitive. Let’s start by defining a couple things first: UI (User Interface) is the means by which the user and a computer system interact. For the purpose of this article we … Continued



The Value of Robot-Centered Design

Designers, developers, and engineers have spent vast amounts of time and effort understanding how humans interact with technology. Human-Centered Design The study of human factors guides our knowledge of how our behaviors, beliefs, education level, and past experiences influence how we accept and work with digital tools. Those who create computers, mobile devices, software, hardware, … Continued