Implementation of github project Performance 1 - Part-2

From Part 1 the rest of the steps will go here in part 2.

Step 01: Creating PostController using artisan command

php artisan make:controller PostController

Step 02: Putting index method in PostController

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::all();

        return view('posts.index', compact('posts'));
    }
}   


Step 03: Creating blade filesĀ 

resources/views/app.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel Performance</title>

        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />

    </head>
    <div class="app-body">
        <main class="main">

            <div style="padding-top: 20px" class="container">
                @yield('content')
            </div>
        </main>
    </div>
    </body>
</html>


Then, Resources/views/posts/index.blade.php

@extends('app')
@section('content')
<div class="card">
    <div class="card-header">
        All Posts
    </div>

    <div class="card-body">
        <div class="table-responsive">
            <table class=" table table-bordered table-striped table-hover">
                <thead>
                    <tr>
                        <th>
                            Title
                        </th>
                        <th>
                            Author
                        </th>
                        <th>
                            Email
                        </th>                                                
                    </tr>
                </thead>
                <tbody>
                    @foreach($posts as $post)
                        <tr >
                            <td>
                                {{ $post->title ?? '' }}
                            </td>
                            <td>
                                {{ $post->user->name ?? '' }}
                            </td>
                            <td>
                                {{ $post->user->email ?? '' }}
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection


Step 04: Specifying route in routes/web.php

Route::get('/', [PostController::class, 'index'])->name('post.index');


Then if you run artisan command php artisan serve and browse to http://localhost:8000 you will see list of posts with laravel debugbar at the bottom.

In debugbar it shows following for eloquent queries:

1. views (2)
2. Queries (31)
3. Models (60) ( here Inside Model tab.. it shows User model is used 30 times and Post model is used 30 times )
4. Memory usage (20 MB)
5. Request duration 386ms

Step 05: Now if you use eager loading in eloquent like following the above values gets changed significantly

// instead of this 
$posts = Post::all();
// using this with eager loading reduces number of queries and models in debugbar
$posts = Post::with('user')->get();


Now In debugbar it shows following for eloquent queries:

1. views (2)
2. Queries (2)
3. Models (40) ( however, here Inside Model tab.. it shows User model is used 10 times and Post model is used 30 times )
4. Memory usage (20 MB)
5. Request duration 277ms

So, using eager loading reduces number of queries and models great deal in debugbar

Another approach also can be taken without using eager loading but loading relationship later with eloquent load() method

$posts = Post::all();
//loading relationship later to optimize query performance like eager loading
$posts->load('user');


With above the same performance optimization can be achieved like with eager loading. It shows same numbers in debugbar which has been shown with eager loading.

To go back to part 1 click here

Related Posts