Skip to content

Instantly share code, notes, and snippets.

@hamog
Last active April 20, 2017 14:46
Show Gist options
  • Save hamog/013b0fdf81d1ba83b922a6349daa3e1e to your computer and use it in GitHub Desktop.
Save hamog/013b0fdf81d1ba83b922a6349daa3e1e to your computer and use it in GitHub Desktop.
Laravel Optimization

Laravel Optimization

Adding up to all the tips you received here, here is what I generally do to increase my app's performance

1-Use a session driver like redis or memcached

2-Use one of these drives for caching also

3-Use cached query results for all the queries that are not volatile (not subject to change frequently)

4-Fine-tune all Eloquent queries to eager-load any related model whenever it is possible and only when needed, I generally log out every query while developing and double-check each request. Use Model::with(...) or $instance->load(...) to eager-load your relations, but usage of the protected $with; property on your models should be done with a lot of caution.

$books = App\Book::with('author')->get();

foreach ($books as $book) {
    echo $book->author->name;
}

For this operation, only two queries will be executed:

select * from books

select * from authors where id in (1, 2, 3, 4, 5, ...)

5-Comment out any provider not used, and as said before prefer to use deferred providers (look in the docs)

6-Disable Query Logging on production

// on AppServiceProvider, or on a custom provider
public function boot()
{
    if ( env( 'APP_ENV', 'local' ) !== 'local' )
    {
        \DB::connection()->disableQueryLog();
    }
}

7-Run these commands on production server

composer dump-autoload
php artisan clear-compiled
php artisan config:cache
php artisan route:cache
php artisan optimize --force

8- Minify CSS & JS files

//Laravel Elixir
gulp --production

//Laravel Mix
npm run production
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment