Optimizing your Laravel application for performance and readability
- Eagerloading and nested Eagerloading.
- Compilation of your asset using Laravel mix.
- Caching your application in production mode
- Implementing lazyload or lazy sizes for images.
- Avoid writing bad queries / optimizing your queries.
- Indexing your database.
- Implement the use of helpers, services, or traits.
- Laravel Debugbar for query inspection.
- Try your best to simplify your solution with the best algorithm.
- Upgrading your application.
Eagerloading and Nested Eagerloading: This is one of the interesting features that Laravel comes with, it helps you optimize your queries. For instance, if you want to get 10 users and all their posts without eager loading you will run close to 10–200 queries depending on the number of posts each user has.
//In your controller
$users= User::take(10)->get();
{{ — In your view — }}
@foreach($users as $user){{$user->name}}@foreach($user->posts as $post){{$post->title}}
@endforeach
@endforeach
For instance if each user has 5 post: (1+5)*10 = 60 queries. But if we implement Eagerloading here we can reduce the queries into n+1. In your controller just add
$user = User:: with(‘posts’)->take(10)->get();
This will reduce the queries into (1+1)*10 = 20. Instead of 50 queries.
P.S: Before using Eagerloading you should have created a relationship between your User model and your Post model.
Nested Eagerloading: it comes in handy when you are building a REST API using Laravel. For instance, we have 3 tables (Orders, Order_items, Products) from the Order model you can get an item added to a particular cart and also get the product name, id, and other entities, etc of that particular item.
$data = Order::with('items.product')->get();
Using Laravel Mix comes in very handy most times for both large and small projects, it helps you reduce the size of your assets drastically both your JavaScript and Sass assets especially if you are using Vue on your Laravel application.
Install node modules
npm install
Compile your asset from your resource folder into your pubic folder using
npm run dev or npm run watch
npm run dev or npm run watch
When you want to deploy your application run
npm run prod
PS: This command will reduce all your assets into a minified version, ensure you remove packages you don’t’ use anymore before pushing to your production server.
Caching your application route, config, views in production mode helps increase your application speed but sometimes it could cause multiple issues when debugging in production mode because some changes might not be effected due to the cache.
You can cache your application with:
php artisan optimize
Remove cache using:
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
Lazyloading images with JQuery or Vuejs: Building a project like an e-commerce or gallery site you will need to lazyload some of the images to increase the page speed, if you are using Laravel blade template for the frontend you can use various JQuery packages available on Github to achieve this if the project is a Single Page Application built with Vue or React you can also check npm for Lazyload or skeleton loading package available for your desired frontend framework.
Optimizing your queries: Writing bad queries sometimes seem inevitable because most times we are more focused on solving the problem with the possible solution rather than solving the problem with the most efficient solution, but at some point, you will need to consider refactoring your code after achieving the solution, for performance and maintenance purposes.
If you are a PHP/Laravel developer and you enjoy using SQL, you can optimize queries using EverSQL for free.
https://www.eversql.com/
Indexing your database through migration: Indexing is one of the most common solutions for database optimization. An index is a data structure that enables swift retrieval of data, MYSQL Database supports indexing. You can create indexes for your table through migration.
1. Plain index
$table->index(‘slug’);
2. Unique index
$table->string(‘email)->unique();
https://laravel.com/docs/5.8/migrations#indexes
Do not repeat yourself: Avoid writing the same block of code multiple times instead create helpers, services, classes, or traits for codes you will use very often. it helps u reduce the lines of code and it makes your code maintainable since you can make changes from one place.
The Laravel Debugbar by Barryvdh: It is a package that allows you to quickly and easily keep tabs on your application during development. With a simple installation and powerful features, the Debugbar package is one of the cornerstone packages for Laravel. You can use it to trace your views, mails, exception, queue, and route. With this package, you can easily identify possible mistakes and optimize them for maximum performance. It also shows your page size and page speed etc. Sometimes your page speed may depend on your machines’ speed as well but try your best to optimize the application.😉
Most times the first solution that comes to your head isn’t always the best, you just need to brainstorm to find the most efficient solution to tackle the existing problem. If you are working on a project with a tight deadline u can try solving the problem for the moment, then later you should consider refactoring your code and algorithm into something simpler and more efficient.
Upgrading your Laravel application and PHP version: most people neglect this part in application development, over the years a lot has changed as time went by, avoid using PHP 5 in your projects, try upgrading your server PHP version to PHP 7.2, 7.3 or 7.4, you can also consider upgrading your Laravel application to Laravel 5.8 or 6.0 though it’s a hectic process, but it’s worth the shot.