Use Eloquent's CRUD methods, query builder, aggregates, and findOrFail for database access.
Eloquent ORM Basics
// Model
class Post extends Model {
protected $fillable = ["title", "body", "slug", "user_id"];
protected $casts = [
"published_at" => "datetime",
"is_draft" => "boolean",
];
}
// CRUD
$post = Post::create(["title" => "Hello", "body" => "World"]);
$post = Post::find(1);
$post = Post::findOrFail(1); // 404 if missing
$post = Post::where("slug", "hello")->firstOrFail();
$post->update(["title" => "Updated"]);
$post->delete();
// Query builder
Post::where("is_draft", false)
->where("published_at", "<=", now())
->latest()
->take(5)
->get();
// Aggregates
Post::count();
Post::max("views");
Post::where("user_id", 1)->sum("views");