Database & EloquentCore· 40 min read

Eloquent Models

Laravel’s beautiful database layer — work with tables as simple objects, no SQL needed.

What you will learn

  • Create an Eloquent model
  • Read data with model methods
  • Understand the ORM idea

A table becomes an object

Eloquent is Laravel’s ORM (Object-Relational Mapper — a tool that lets you work with database rows as if they were ordinary code objects, so you write methods instead of SQL). Each table gets a model class, and you query it with simple methods — no SQL strings.

Generate a model
php artisan make:model Product

This creates a small model class at app/Models/Product.php. You do not have to tell it which table to use — by Laravel’s naming convention, the Product model automatically talks to the products table (singular model name, plural table name).

Now you can read data from that table using simple method calls on the model — no SQL strings:

Query with Eloquent — no SQL needed
use App\Models\Product;

$all = Product::all();             // SELECT * FROM products
$one = Product::find(1);           // find by id
$cheap = Product::where('price', '<', 500)->get();
$latest = Product::orderBy('created_at', 'desc')->take(5)->get();

Each line is an Eloquent query, and the comments show the SQL it stands in for. Product::all() fetches every row. Product::find(1) fetches the one row whose id is 1. Product::where('price', '<', 500)->get() fetches only products cheaper than 500 — you chain ->get() to actually run it. orderBy('created_at', 'desc')->take(5)->get() sorts newest first and keeps just 5. You read it almost like an English sentence, and Eloquent writes the SQL behind the scenes.

Reading data with Eloquent always follows the same few steps:

  1. Run php artisan make:model Product once to create the model class for your table.
  2. Bring the model into your file with use App\Models\Product; so you can call it.
  3. To read all rows, call Product::all(); to read one row by its id, call Product::find($id).
  4. To read a filtered set, chain conditions like ->where(...), ->orderBy(...) or ->take(...), then finish with ->get() to actually run the query.
  5. Use the returned data — a single model, or a collection you can loop over in a Blade view.

Note: By convention, the Product model maps to the products table (singular model → plural table). Eloquent writes the SQL for you — compare Product::all() to the raw SELECT * FROM products you wrote with PDO.

Tip: Everything you learned in SQL still applies — where, orderBy, joins — but now as clean, chainable method calls. Understanding SQL means you know exactly what Eloquent generates.

Q. What is Eloquent?

Answer: Eloquent is Laravel’s ORM: each table maps to a model class you query with expressive methods like Product::all().

✍️ Practice

  1. Create a model and fetch all records with ::all().
  2. Query with where() and orderBy().

🏠 Homework

  1. Create a Post model and write three different Eloquent queries.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →