How do you define a basic Eloquent model in Laravel, and how does it automatically map to a corresponding database table?
Short answer
An Eloquent model is a PHP class that extends Laravel's base Model class, and by convention, Eloquent automatically assumes the model corresponds to a database table with the plural, snake case version of the model's class name, meaning a model named Product automatically maps…
Advertisement
AModel answer
An Eloquent model is a PHP class that extends Laravel's base Model class, and by convention, Eloquent automatically assumes the model corresponds to a database table with the plural, snake case version of the model's class name, meaning a model named Product automatically maps to a table named products, letting you immediately start querying and saving data without writing any explicit mapping configuration for the common case.
class Product extends Model {
}
$product = Product::find(1);
$products = Product::where('price', '>', 100)->get();Real-world example
A developer creates a new Product model class, and Eloquent automatically knows to look for a products table in the database, letting the developer immediately start querying products without any additional configuration.
Likely follow-ups
- How do you override Eloquent's default table name convention for a specific model?
- What naming convention does Eloquent use for a model's primary key column?
Related topics