What is Eloquent, and how does it provide an intuitive, Active Record based way to interact with your application's database within Laravel?
Short answer
Eloquent is Laravel's built in ORM, letting each database table be represented by a corresponding model class, and following the Active Record pattern, each model instance directly represents a single database row and includes convenient built in methods for querying, creating…
Advertisement
AModel answer
Eloquent is Laravel's built in ORM, letting each database table be represented by a corresponding model class, and following the Active Record pattern, each model instance directly represents a single database row and includes convenient built in methods for querying, creating, updating, and deleting records, along with a fluent, chainable query builder syntax that makes even fairly complex database queries readable and easy to construct.
$products = Product::where('price', '>', 50)->orderBy('name')->get();
$product = Product::create(['name' => 'Widget', 'price' => 29.99]);Real-world example
A Laravel application uses Eloquent's fluent query builder to construct a readable, chainable query filtering and sorting products, without needing to write a single line of raw SQL for this common type of database interaction.
Likely follow-ups
- How does Eloquent's query builder prevent SQL injection automatically?
- What is the difference between using Eloquent models and Laravel's lower level query builder directly?
Related topics