Laravel Models & Eloquent ORM – Beginner’s Guide

Laravel Models & Eloquent ORM – Beginner’s Guide

What is a Model in Laravel?

A Model represents a table in your database and lets you interact with its data.
Laravel uses Eloquent ORM (Object Relational Mapping), so you can work with database records like PHP objects — no need to write raw SQL for basic operations.


Creating a Model and Migration

Let’s store contact form submissions in a database.

Run this Artisan command:

php artisan make:model Contact -m

This will create:

  1. Modelapp/Models/Contact.php
  2. Migrationdatabase/migrations/xxxx_xx_xx_create_contacts_table.php

Defining Database Columns

Open the migration file in database/migrations/ and update the up() method:

public function up(): void
{
    Schema::create('contacts', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email');
        $table->timestamps();
    });
}

Run the migration:

php artisan migrate

Now you have a contacts table in your database.


Saving Data to the Database

In ContactController.php, update the submitForm method:

use App\Models\Contact;

public function submitForm(Request $request)
{
    $request->validate([
        'name'  => 'required|min:3',
        'email' => 'required|email'
    ]);

    // Create new contact record
    $contact = new Contact();
    $contact->name  = $request->name;
    $contact->email = $request->email;
    $contact->save();

    return "Contact saved successfully!";
}

Mass Assignment (Shorter Way to Save)

Instead of setting each property manually, you can use mass assignment.

Step 1 – Allow mass assignment in the model:

class Contact extends Model
{
    protected $fillable = ['name', 'email'];
}

Step 2 – Save directly:

Contact::create($request->only(['name', 'email']));

What $fillable does

In short

  • Security: Prevents malicious or accidental overwriting of sensitive columns.
  • Convenience: Lets you use mass assignment without manually assigning each property.
Contact::create([
'name' => 'John Doe',
'email' => 'john@example.com',
'is_admin' => true // ❌ This will be ignored
]);

Only name and email will be inserted into the database; is_admin will be ignored.

$fillable is a whitelist — it tells Laravel:

“These are the only fields I allow to be mass-assigned.”


Retrieving Data from the Database

In a controller method:

$contacts = Contact::all();
return view('contacts_list', ['contacts' => $contacts]);

In the view (contacts_list.blade.php):

<h1>Contact List</h1>
<ul>
    @foreach($contacts as $contact)
        <li>{{ $contact->name }} - {{ $contact->email }}</li>
    @endforeach
</ul>

Common Eloquent Methods

MethodDescription
all()Get all records
find($id)Find a record by ID
where(‘field’, value)->get()Filter records
create([…])Insert a new record
update([…])Update a record
delete()Delete a record

Conclusion:
We can now create models, run migrations, save data, and retrieve it — the core skills for building a database-powered Laravel app.