Laravel 8 Tutorial - Eloquent

In this tutorial, we are going to learn about Eloquent, the Object Relational Mapper (ORM) that comes packaged with the PHP Laravel framework.

Eloquent provides an extremely easy way to communicate with a database, using an architectural pattern where the model created in the Model-View-Controller (MVC) structure corresponds to a table in the database.

The advantage of using Eloquent is that models can perform common database operations without coding lengthy SQL queries. Models allow data querying in your tables as well as inserting new records into tables.

Let's understand the Eloquent system by creating a new Laravel project.

First, go to phpMyAdmin and create a new database. Open the URL localhost/phpmyadmin in your browser and click on "Databases". From here, let's create a new database named "studentdb".

Now, configure this database in the .env file in your Laravel project. Open the .env file and add the database details:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=studentdb
DB_USERNAME=root
DB_PASSWORD=

Save the file. Now, let's create an Eloquent model with migration.

In the command prompt, run the command:


php artisan make:model Student –m

Switch to the project and go inside the database directory, then open the migrations directory. Here, you can see the create_student_table migration.

Open it and let's add some columns:


$table->string('name');
$table->string('email');
$table->string('phone');

Now, let's migrate the migration. In the command prompt, run the command:


php artisan migrate

Let's see inside the database. Refresh the database and you can see that the tables are created.

Now, insert some records inside the table using the database seeder. Let's insert some dummy records using Faker:


public function run()
{
$faker = Faker::create();
foreach (range(1,100) as $index) {
DB::table('students')->insert([
'name' => $faker->name,
'email' => $faker->email,
'phone' => $faker->phoneNumber.
]);
}
}

Run the seeder. In the command prompt, run the command:


composer dump-autoload && php artisan db:seed

Alright, now you can see that 100 records are inserted.

Now, let's create a controller. In the command prompt, run the command:


php artisan make:controller StudentController

Go to the StudentController and let's use the model. First, import the Student model:


use App/Models/Student;

Create a function for fetching all records:


public function fetchStudents()
{
$students = Student::all();
retun $students;
}

Now, create the routes:


Routes::get('/students',[StudentController::class, 'fetchStudents']);

Let's check. Type /students in the browser and you can see all the students.

Now, let's see the where clause in Eloquent. Go to the controller and add a condition:


public function fetchStudents()
{
$students = Student::where('id',3)->get();
retun $students;
}

Save and let's check. Switch to the browser and refresh the page. You can see the record whose id is 3.

Now, let's see whereBetween:


public function fetchStudents()
{
$students = Student::whereBetween(id', [40, 50])->get();
retun $students;
}

It will fetch all records whose id is between 40 and 50. Let's check. Refresh the page and you can see the result.

Now, let's see orderBy:


public function fetchStudents()
{
$students = Student::orderBy('id','DESC')->get();
retun $students;
}

For ascending order, write ASC and for descending order, write DESC. Let's check. Refresh the page and you can see the records in descending order.

So, in this way, you can use Eloquent in Laravel 8.