Working with web applications always creates this question. That if the application loads on time as quickly as possible. As it is one of the major concern when it comes to SEO ( Search Engine Optimisation ). Let's see how you can make your Laravel/ php application fast enough.
It is true. Your code matters a-lot. when it comes to performance of application. Have a look on this code.
//This query getting id, title, image, content, slug, author_id, created_at, updated_at Post::get()
We can change this code to
//If i need only these fields then this is enough Post::get(['id','title','image'])
You can see you can optimize your query. Also see this example.
$user = []; //Getting user id from posts foreach($posts as $post){ if($post->user_id){ //querying for each post to get user info from database $user[] = User::where('id',$post->user_id)->get(); } }
Now in this code if you see carefully. We are getting user id from each post. and then querying inside loop by passing that user id. It will take time if you have a-lot of posts. It would be better if you change query of this kind to something like this. And avoid passing query inside a loop.
$user = []; //get all user id first foreach($posts as $post){ if(!in_array($post->id,$user)){ $user[] = $post->id; } } //get user data using user array we got and only taking required fields return User::whereIn('id',$user)->get(['id','name','ip_address']); or //get user data using user array we got and only taking required field return User::whereIn('id',$user)->pluck('id');
Check here we are correcting 3 things.
1. We are only getting users id in array once so if many posts saved by one user we will have that user in our $user variable only once because of in_array php function. And it will help us to run query only once for each user.
2. We are getting only required fields not every field so we will have less data to process.
3. We are returning results straight away.
return User::whereIn('id',$user)->pluck('id'); //instead $data = User::whereIn('id',$user)->pluck('id'); return $data;
Don't use same code again and again. Use components Instead. Let's say we are using below given code in many blade files ( pages ).
//Getting user name and displaying@foreach($users as $user){ <p></p>}
So you can put it in a component. Just create components folder in Laravel proect and create a component. Create users.blade.php component file. And put code in there. Now when ever you call that component code will display users and you can change only one file and changes will reflect where ever you call that component.
//call component @component('users') @endcomponent
Now you know how to code like a pro it's time to think twice before adding another package or library in our project. Laravel applications divided into 2 parts. layout files and blade files. Blade files extend layout files. So it is vital to have light layout files because each library you call in layout file will load on each page of your application. So use best practices. Let's see example of layout file here
<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> <link rel="stylesheet" href=""> @yield('styles') <style> body { font-family: "Roboto",sans-serif; font-weight: 300; margin-top: 110px !important; } </style> </head> <body> @yield('content') <script src=""></script> <script src=""></script> <script src=""></script> <script src=""></script> <script src=""></script> <script src=""></script> <script src=""></script> @yield('scripts') </body> </html>
Now if you see this layout file. We are including few script files. and as it is a layout file these scripts will load on each page of our application. So what we can do here.
If we are using owl carousel on one blade file then only include in that blade file. Not on this file.
One good thing about above layout file is most script files are minified ( Compressed ). This is good minified files can be used to increase performance. So always use minified version of scripts.
In laravel we can compress our css and js files. To do that in our laravel application open webpack.mix.js file using text editor. Add instruction check this example.
//Minify the css files mix.minify(['public/frontendNew/css/bootstrap.css']); mix.minify(['public/frontendNew/css/owl.carousel.css']); mix.minify(['public/frontendNew/css/font-awesome.min.css']); //Minify the js files mix.minify(['public/frontendNew/js/vendor/jquery-2.2.4.min.js']); mix.minify(['public/frontendNew/js/vendor/bootstrap.min.js']); mix.minify(['public/frontendNew/js/owl.carousel.min.js']);
Here we told laravel to minify our files. But it will take effect when we run npm command to make our ready for production.
npm run production
We also can compile multiple files into one using Laravel Mix see here
//making minified file from two style css files mix.styles([ 'public/css/vendor/styleOne.css', 'public/css/vendor/styleTwo.css' ], 'public/css/all.css');
Read More about Laravel Mix Here
It is also important to run code when it is needed not on each page refresh. Users looks for info then user clicks then display content not on page refresh. Only load data on page load when it is must. check this example
//Getting comments and replies all at once displaying it $commentIds = [];@foreach($comments as $comment){ <p></p> $commentIds[] = $comment->id;}return Reply::whereIn('comment_id',$commentIds)->get(['id','reply','user_id']);
In this code we are displaying comments and replies all at once to make things faster you can loan replies on click of comment. So to see replies user clicks on each comment then use Ajax to display replies.
If you load images on page then obviously page will load slow so it is better to compress images before adding to your content. Use this tool to define Image resolution and compress .
To optimize Laravel Application there are laravel commands as well that developers use during development.
php artisan route:cache php artisan config:cache php artisan optimize –force php artisan config:clear php artisan route:clear php artisan optimize:clear
So if you have cache and you want to clear it you can also use these commands as well.
Let me know if you liked these ways to make your application faster. and let me know if there is any other way you use in your Laravel application.