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
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'];
}
$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\Modelsdirectory and extendIlluminate\Database\Eloquent\Model. - The table name is the "snake case", plural name of the class unless you set
$table:AirTrafficControllermaps toair_traffic_controllers. - Eloquent assumes a primary key column named
id; set$primaryKeyto change it. - Eloquent automatically sets
created_atandupdated_at; set$timestampstofalseto turn this off.
Creating and finding
createsaves a new model in a single statement, but first you must specify afillableorguardedproperty, because all Eloquent models are protected against mass assignment vulnerabilities by default.firstOrCreateinserts a record if none matches;firstOrNewreturns 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.
License: CC-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.
Cite this
iinventedlight. 2026. "Laravel Eloquent models map a class to a table by convention; use fillable for mass assignment." iinventedlight Library, iil:note:pkbc07dm, version of 2026-11-18. https://iinventedlight.com/library/notes/laravel-eloquent-models/. License: CC-BY-4.0.
@misc{iil_note_pkbc07dm,
author = {{iinventedlight}},
title = {{Laravel Eloquent models map a class to a table by convention; use fillable for mass assignment}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/laravel-eloquent-models/},
note = {Permanent ID iil:note:pkbc07dm. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/laravel-eloquent-models.md.