ORM in Laravel

Beginner’s Guide to ORM – What It Is and How It Works in Laravel and Other PHP Frameworks

If you’re new to Laravel (or even PHP in general), you might have come across the term ORM.

You might be wondering:

  • What is ORM?
  • Do other PHP frameworks use it?
  • Why does it feel similar to a query builder?
  • What’s the difference between ORM and raw SQL queries?
  • What’s the deal with fillable properties in Laravel models?

Let’s break it all down in a beginner-friendly way.


What is ORM?

ORM stands for Object Relational Mapping.

That sounds fancy, but the idea is simple:
ORM allows you to interact with your database tables using objects (usually PHP objects). Instead of writing SQL queries directly, you can use PHP code to create, read, update, or delete rows.

Example – Without ORM (Raw SQL)

$sql = "SELECT * FROM products WHERE price > 500";
$result = mysqli_query($connection, $sql);

Example – With ORM (Eloquent in Laravel)

$products = Product::where('price', '>', 500)->get();

With ORM, you talk to your database using PHP objects and methods, rather than plain SQL.


Why is It Called “Object Relational Mapping”?

Let’s break down the term:

TermMeaning
ObjectPHP class representing a table row (like a Product object)
RelationalRefers to Relational Databases like MySQL, PostgreSQL
MappingLinking database columns to object properties

Example

If your products table has columns: id, name, price, Laravel (using Eloquent ORM) maps them to a Product object like:

$product = Product::find(1);

echo $product->name; // This "name" comes from the "name" column in the database
echo $product->price; // This "price" comes from the "price" column

The table row is “mapped” into a Product object — this is why it’s called Object Relational Mapping.


Is ORM Used Outside of Laravel?

Short Answer: Yes!

ORM is a concept, not something unique to Laravel. Almost every modern framework has its own ORM.

Here are some examples of ORMs in other PHP frameworks:

FrameworkORM
LaravelEloquent
SymfonyDoctrine
CakePHPCake ORM
YiiActive Record

So yes — even outside Laravel, PHP frameworks use ORM systems to handle database work more easily.


Can Eloquent Be Used Outside Laravel?

Yes, but…

Technically, you can use Eloquent outside of Laravel. Since Eloquent is just a package, you could install it in a non-Laravel project using Composer.

However, most developers don’t do this because:

  • Eloquent is very tied to Laravel’s ecosystem (service container, facades, etc.).
  • Other frameworks (like Symfony) already have their own ORMs (like Doctrine).

That said, if you really love Eloquent, you could set it up in a plain PHP project, but it’s a bit of extra work.


What Are Some Other ORMs in PHP?

Here are a few popular ones:

ORMDescription
DoctrineThe most powerful and flexible ORM, often used in Symfony projects. Handles complex relationships very well.
PropelAnother ORM that supports Active Record and Data Mapper patterns.
RedBeanPHPA lightweight ORM designed for smaller projects with minimal configuration.
EloquentLaravel’s default ORM — simple, elegant, and beginner-friendly.

What Are Fillable Properties in Laravel Models?

What is $fillable?

$fillable is a security feature in Laravel models.

It is a list of columns you allow mass assignment for.
Mass assignment means you insert data directly from an array.

Example

In your Product model:

phpCopyEditclass Product extends Model
{
    protected $fillable = ['name', 'price'];
}

This means you are allowed to do:

Product::create([
    'name' => 'Phone',
    'price' => 599.99
]);

But if you accidentally try to insert some unauthorized columns (like is_admin), they will be ignored — because they’re not in $fillable.

Why Use $fillable?

  • To protect your database from unexpected columns being inserted (especially from form data).
  • Helps avoid mass assignment vulnerabilities.

Final Recap

ConceptExplanation
ORMMap database tables to PHP objects, interact with data using objects instead of raw SQL.
Object Relational MappingConverts database rows into objects with properties matching columns.
Eloquent outside Laravel?Possible, but not common — most projects stick with the framework’s own ORM.
Other ORMs in PHPDoctrine, Propel, RedBeanPHP.
$fillableControls which columns are allowed for mass assignment to protect your data.

Awesome! Here’s a simple visual diagram to help you understand how ORM works in Laravel (or any framework that uses an ORM). Below the diagram, I’ll also explain the flow.

+-----------------------+
|  products Table       |
+-----------------------+
| id   | name  | price  |
|----- |------ |------- |
| 1    | Phone | 500.00 |
| 2    | Laptop| 1200.00|
+-----------------------+

      ⬇️ ORM Maps This Row to an Object ⬇️

+----------------------+
| Product (PHP Object) |
+----------------------+
| id    = 1            |
| name  = "Phone"      |
| price = 500.00       |
+----------------------+

PHP Code Example:
$product = Product::find(1);

echo $product->name;  // Output: Phone
echo $product->price; // Output: 500.00

Explanation

  • Your MySQL table has rows (each row = 1 product).
  • Laravel’s Eloquent ORM fetches those rows and maps them into PHP objects.
  • Each object’s properties match the table’s columns.
  • You work with objects in PHP instead of writing SQL.

How the Flow Works

StepDescription
1You run Product::find(1) in your controller.
2Eloquent ORM writes the SQL query for you: SELECT * FROM products WHERE id = 1
3Database returns a row with columns: id, name, price.
4Eloquent maps the row into a Product object.
5You access the data using $product->name instead of $row['name'].

Why This is Helpful

✅ You don’t have to write SQL manually.
✅ You work with objects that feel like regular PHP classes.
✅ You can easily apply filters, relationships, and sorting using methods instead of query strings.


Bonus – Visualizing Fillable

If you try to mass assign (insert an array), only the fillable columns are accepted.

Product::create([
    'name' => 'Tablet',
    'price' => 300,
    'is_admin' => true // ❌ This is ignored if not in $fillable
]);

If Product model has:

protected $fillable = ['name', 'price'];

The is_admin column will be ignored — even if it’s accidentally sent from a form.

Conclusion

Laravel’s Eloquent is one of the easiest ORMs for beginners. But the concept of ORM exists everywhere — in PHP frameworks, Python (Django ORM), and even Java (Hibernate). Understanding the basics will make you a better backend developer, no matter which language or framework you use.