---
source: https://iinventedlight.com/library/notes/laravel-eloquent-models.md
title: "Laravel Eloquent models map a class to a table by convention; use fillable for mass assignment"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (Laravel docs); MIT (laravel/docs (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Laravel docs, Eloquent: Getting Started; laravel/docs (GitHub), eloquent.md (source page). MIT (https://raw.githubusercontent.com/laravel/docs/12.x/license.md). Copyright Taylor Otwell."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: laravel eloquent models"
tags: [laravel, php, eloquent, orm, database, models]
client: Librarian
sources:
  - id: S1
    title: "Eloquent: Getting Started"
    publisher: Laravel docs
    url: https://laravel.com/docs/12.x/eloquent
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "eloquent.md (source page)"
    publisher: laravel/docs (GitHub)
    url: https://github.com/laravel/docs/blob/12.x/eloquent.md
    accessed: 2026-11-18
    license: "MIT"
---
# Laravel Eloquent models map a class to a table by convention; use fillable for mass assignment
**Answer:** Generate a model with `php artisan make:model Flight -m`, then query it like a query builder, for example `Flight::where('active', 1)->orderBy('name')->get()`. By convention the `Flight` model uses the `flights` table, an `id` primary key, and `created_at` and `updated_at` timestamps.

## Example

```php
<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Flight extends Model
{
    use SoftDeletes;
    protected $table = 'my_flights';
    protected $primaryKey = 'flight_id';
    protected $fillable = ['name'];
}
```

```php
$flights = Flight::where('active', 1)->orderBy('name')->limit(10)->get();
$flight = Flight::create(['name' => 'London to Paris']);
$flight = Flight::firstOrCreate(['name' => 'London to Paris']);
Flight::find(1)->delete();
```

## Conventions

- Models typically live in the `app\Models` directory and extend `Illuminate\Database\Eloquent\Model`.
- The table name is the "snake case", plural name of the class unless you set `$table`: `AirTrafficController` maps to `air_traffic_controllers`.
- Eloquent assumes a primary key column named `id`; set `$primaryKey` to change it.
- Eloquent automatically sets `created_at` and `updated_at`; set `$timestamps` to `false` to turn this off.

## Creating and finding

- `create` saves a new model in a single statement, but first you must specify a `fillable` or `guarded` property, because all Eloquent models are protected against mass assignment vulnerabilities by default.
- `firstOrCreate` inserts a record if none matches; `firstOrNew` returns a new, unsaved model instance instead.
- Each Eloquent model works as a query builder, so the query builder methods are available.

## Deleting

Call `delete` on a model instance. With the `SoftDeletes` trait, the `deleted_at` attribute is cast to a `DateTime` / `Carbon` instance, and you should add a `deleted_at` column to the table.

## Generating

`make:model` with `--migration` or `-m` also creates a database migration; other options generate factories, seeders, policies, controllers and form requests.

## See also


## Claims

- id: C1
  claim: The table name defaults to the snake case plural class name.
  source: S2
  evidence: "the \"snake case\", plural name of the class will be used as the table name"

- id: C2
  claim: Eloquent assumes a primary key column named id.
  source: S2
  evidence: "primary key column named `id`"

- id: C3
  claim: Models are protected against mass assignment vulnerabilities.
  source: S1
  evidence: "all Eloquent models are protected against mass assignment vulnerabilities"

## Sources

- [S1] Eloquent: Getting Started. Laravel docs. https://laravel.com/docs/12.x/eloquent . License: MIT. Accessed 2026-11-18.

- [S2] eloquent.md (source page). laravel/docs (GitHub). https://github.com/laravel/docs/blob/12.x/eloquent.md . License: MIT. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Laravel docs, Eloquent: Getting Started; laravel/docs (GitHub), eloquent.md (source page). MIT (https://raw.githubusercontent.com/laravel/docs/12.x/license.md). Copyright Taylor Otwell. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
