Eloquent is Laravel's ORM (Object-Relational Mapper). Each database table has a model class, and each row becomes an object.
Create a model
php artisan make:model Post -m creates app/Models/Post.php and its migration. By convention the Post model uses the posts table.
Reading data
Post::all()— every rowPost::find(1)— one row by id, ornullPost::findOrFail(1)— one row, or a 404 pagePost::where('is_published', true)->latest()->get()— filtered and sortedPost::where('slug', $slug)->first()— first matchPost::count()— number of rows
Mass assignment
Post::create([...]) saves a row from an array. For safety, list allowed columns in $fillable; any other key is ignored. This stops users from setting fields like is_admin through a form.
Pagination
Post::latest()->paginate(10) returns 10 rows per page, and {{ $posts->links() }} in Blade prints the page links.
Example
<?php
// app/Models/Post.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = ['user_id', 'title', 'body', 'is_published'];
}
// Anywhere in a controller:
$post = Post::create([
'user_id' => 1,
'title' => 'Hello Eloquent',
'body' => 'My first post.',
]);
$published = Post::where('is_published', true)->latest()->get();
echo $post->title; // Hello Eloquent
create() inserts a row and returns a Post object. Columns are read as properties, e.g. $post->title.