DEV Community

Morcos Gad
Morcos Gad

Posted on

Quick & Easy Ways to Speed - Laravel Website

It's great, fast and useful ways I found in this wonderful article
https://ashallendesign.co.uk/blog/6-quick-and-easy-ways-to-speed-up-your-laravel-websiteto help you make your project as fast as possible. 6 tips I hope you will benefit from them in your future projects.

  • Only fetch the fields you need in your database queries
$users = User::all();

foreach($users as $user) {
    // Do something here
}

// Now
$users = User::select([id, first_name, last_name])->get();

foreach($users as $user) {
    // Do something here
}
Enter fullscreen mode Exit fullscreen mode
  • Use eager loading wherever possible
$comments = Comment::all();

foreach ($comments as $comment ) {
    print_r($comment->author->name);
}

// Now
$comments = Comment::with('authors')->get();

foreach ($comments as $comment ) {
    print_r($comment->author->name);
}
Enter fullscreen mode Exit fullscreen mode
  • Get rid of any unneeded or unwanted packages

Open up your composer.json file and look through each of your dependencies. For each of your dependencies, ask yourself "do I really need this package?". Your answer is mostly going to be yes, but for some of them it might not be.
Ask yourself "Can I write this code myself and remove this entire package"? Of course, due to time constraints, it is not always possible to write the code yourself because you will have to write it, test it and then maintain it. At least with the package, you're using the open source community to do these things for you. But, if the package is simple and quick to replace with your own code, I'd consider removing it.

  • Cache, cache, cache!
php artisan route:cache
php artisan route:clear
php artisan config:cache
php artisan config:clear

// Caching queries and values
$users = DB::table('users')->get();

$users = Cache::remember('users', 120, function () {
    return DB::table('users')->get();
});
Enter fullscreen mode Exit fullscreen mode
  • Use the latest version of PHP

  • Make use of the queues

One way that you can cut down the performance time is to make use of the Laravel queues. If there’s any code that runs in your controller or classes in a request that isn’t particularly needed for the web browser response, we can usually queue it.

class ContactController extends Controller
{
    /**
     * Store a new podcast.
     *
     * @param    Request  $request
     * @return  JsonResponse
     */
    public function store(ContactFormRequest $request)
    {
        $request->storeContactFormDetails();
        Mail::to('mail@ashallendesign.co.uk')->send(new ContactFormSubmission);

        return response()->json(['success' => true]);
    }
}
Enter fullscreen mode Exit fullscreen mode

To make use of the queue system, we could update the code to the following instead

class ContactController extends Controller
{
    /**
     * Store a new podcast.
     *
     * @param    Request  $request
     * @return  JsonResponse
     */
    public function store(ContactFormRequest $request)
    {
        $request->storeContactFormDetails();

        dispatch(function () {
             Mail::to('mail@ashallendesign.co.uk')->send(new ContactFormSubmission);
         })->afterResponse();

        return response()->json(['success' => true]);
    }
}
Enter fullscreen mode Exit fullscreen mode

I hope you benefit from the article and I wish you a happy code.

Top comments (0)