Laravel Eloquent Performance Patterns - Part 1


Hello, everyone today I start my Laravel eloquent performance series. This is part 1, so stay with us and learn how to optimize your Laravel app performance. The database query is important to optimize your application. Because most of the time we don't know which query slows down our application. Try to fetch big data sometimes it consumes too much time. Sometimes it is noticeable and sometimes it is not. To know about your query and timeline of fetching data this Laravel package is very helpful to know every detail. Install this package in the dev requirement of your Laravel application. ๐Ÿ‘‡

composer require barryvdh/laravel-debugbar --dev

Okay, Let's jump to the project. Here I give an example of the School & Student relationship where our database contains 50 Schools where each school has 1000 Students. So we have 50000 Students in our database.

We have two models School & Student. each model has a relationship with the other. The relation creates each student has a school in the database you see Each Student has a school_id ๐Ÿ‘‡

File location ๐Ÿ“‚
app โžก๏ธ Models โžก๏ธ School.php

class School extends Model
{
    use HasFactory;

    public function student() 
    {
        return $this->hasMany(Student::class);
    }
}

File location ๐Ÿ“‚
app โžก๏ธ Models โžก๏ธ Student.php

class Student extends Model
{
    use HasFactory;

    public function school()
    {
        return $this->belongsTo(School::class);
    }
}

Above part is the relation part between two model. Now in we display 50000 students with simple pagination. So we create the StudentController.php where we fetch 50000 student data. Let's check the controller code ๐Ÿ‘‡

File location ๐Ÿ“‚
app โžก๏ธ Http โžก๏ธ Controllers โžก๏ธ StudentController.php

public function index()
    {
        $students = Student::query()
            ->simplePaginate();
        return view('elequent.part-1.index', compact('students'));
    }

In blade file here is the loop to display the data

@foreach ($students as $student)
    <tr>
        <th scope="row">{{ $student->id }}</th>
        <td>{{ $student->name }}</td>
        <td>{{ $student->school->name }}</td>
        <td>{{ $student->phone_number }}</td>
        <td>{{ $student->state }}</td>
    </tr>
@endforeach

๐Ÿ“ Here you can see we also show the school name of each student. In debugbar you see the models tab 15 School and 16 Student load and in query we have 16 query. So optimize this situation we have to work on controller so we use with() to fetch the school data against each student. Let's update our code,

public function index()
    {
        $students = Student::query()
            ->with('school')
            ->simplePaginate();
        return view('elequent.part-1.index', compact('students'));
    }

Now you reload our blade view and let's check the debugbar. Now you see, in Models tab only 1 school,16 student load, and Queries tab only 2 queries are executed. Okay, now let's fetch the data by order by name. So what happens if we bring the data by name, let's update the controller code.

public function index()
    {
        $students = Student::query()
            ->with('school')
            ->orderBy('name')
            ->simplePaginate();
        return view('elequent.part-1.index', compact('students'));
    }

Now, reload the page again. We see the page load the students data order by alphabetic order. That's cool but we have one problem with that. just check the debugbar and see the time part. you see it takes a little bit more time than without orderBy. So how do we optimize this? Indexing the name table of the database. So now index the name column in the students table, so go to migration file and let's update the name column like this ๐Ÿ‘‡

$table->string('name')->index();

Now migrate the database with artisan command. let's check again, you see the time dramatically change. it takes low time than as usual.

So, that's all check part 2 for more advanced eloquent performance. Click the link below for part 2 ๐Ÿ‘‡

Laravel Eloquent Performance Patterns - Part 2

I am also a big fan of Tailwindcss, so you check out our website for Tailwindcss template and components ๐Ÿ‘‡
tailkitpro.com