Laravel’s Eloquent ORM (Object Relational Mapping)

28 Dec

For web development, Eloquent ORM is extremely useful. It’s quite useful for database operations such as Create, Update, and Delete, as well as any other type of action. Eloquent ORM stamps out the run of a query like PHP for create, update, delete, and other operations. Also, with Eloquent ORM, we can easily retrieve relational data; all we have to do is add a few functions, and it will retrieve records from a database like Pro. We’ll go through various Eloquent ORM topics below.

To  begin, we must first create a model using the following  command:

php artisan make:model User

The Model file will be created with this command. If you are using Laravel 8+, the file can be found on this location app \ Models \ Category.php, If you don’t have a directory named and not using Laravel 8+, the given file will be created on this location-

app \ Category.php


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class User extends Model{

use HasFactory;




Faker can be used to create some sample data that you can use to populate your database. Below is an example of how Faker can help you generate dummy users, contacts, companies, and projects within your project:

php artisan db:seed –class=<Class name of seeder>

Eloquent ORM with Crud

Eloquent ORM with Crud

Using the Laravel Model and Eloquent ORM, you can easily create a CRUD application that lets users create, read, update and delete actions from a database table.

Create Record

Simply create a new instance of the model, set attribute values, and then call the save() method to put a record into the database table.

$data = new User;

$data->name = ” fruits”;

$data->slug = ” fruits”;


Read Record

If you wish to access all records in Laravel Eloquent Model, simply call the all () method:

$data = User::all();

If you want to obtain data that is conditional, use the where () method:

$data = User::where(‘id’,1)->get();

Update Record

To update a record, you must first obtain it and then set the value of the attributes before calling the save () method:

$data = User::find(1);

$data->name = “Fruit”;


Delete Record

To delete a record, simply find the record and call the delete() method:

$data = User::find(1);



Eloquent is a strong object-relational mapping (ORM) that comes pre-installed with the Laravel framework. Many of the complicated queries required to deal with relationships, insert, update, and delete database records, and search, filter, and limit query results are abstracted.

You’ve learned how to use Laravel Crud and how to conduct the basic database operations of insertion, retrieving, updating, and deleting entries in this project-based series.

Have a look at the Laravel official documentation to find out more about Eloquent and all the things you can do with this amazing ORM.


About Shilpa

Backend Developer by Profession! Currently working in BMN Infotech - A Leading Software Company in Amritsar, India! She has experience in Developing Small Scale to Enterprise Scale web applications. Expert in various Programming Languages.

Previous Post

Next Post

Leave a Comment