In this video we are going to learn about Eloquent.
The PHP Laravel framework is packaged with the Eloquent Object Relational Mapper (ORM),
Which provides an extremely easy way to communicate with a database.
It is an architectural pattern where the model created.
In the Model-View-Controller (MVC) structure corresponds to a table in the database.
The advantage is for models to perform common database operations without coding lengthy SQL queries.
Models allow data querying in your tables as well as inserting new records into tables.
Lets understand the echo system of Eloquent.
This is new laravel project.
First of all go to the phpmyadmin and create a new database.
So go to the browser open url localhost/phpmyadmin and here click on databases.
From here lets create a new databse.
Let's say database name is studentdb.
Now configure this database to .env in laravel.
So goto the project just open .env file add the database details here.


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


Alright now save the file.
Now lets create a eloquent model with migration.
So in command prompt run the command.

php artisan make:model Student –m

Now switch to the project and just go inside the database directory then open migrations.
Here you can see the create_student_table migration.
Now just open it and lets add some column here.


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


Now lets migrate the migration.
For that goto the command prompt and here run the command.

php artisan migrate

Now lets see inside the database.
Here just refresh the database.
You can see here tables are created.
Now insert some records inside table so go to the database seeder.
Lets insert some dummy records using faker.
So just type here.

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.
]);
}
}

Now run the seeder.
So goto the command prompt and run the command.


composer dump-autoload && php artisan db:seed


Alright now you can see here 100 records are inserted here.
Now lets create a controller.
So in command prompt run the command.

php artisan make:controller StudentController

Alright now goto to the StudentController.
Now inside this controller lets user use the model.
So first off all just import the Student Model here.


use App/Models/Student;


Create a function for the the fetching all records.

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


Now create the routes


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


Now lets check.
So jut type here /students.
You can see here the all students.
Alrgiht.
Now lets see the where clause in eloquent.
So goto the controller here just add a condition.

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


Save and lets check.
So switch to browser and refresh the page.
You can see here record whose id is 3.
Now lets see whereBetween.

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

It will fetch all records between whose id in between 40 and 50.
Ok lets check.
Just refresh the page and you see the result.
Now lets see the orderBy.

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

For ascending order just write here ASC and for descending order just write here DESC.
Now lets check so refresh the page you can see records in descending order.
So in this way you can use Eloquent in laravel 8.