What is Eloquent ORM?
The Eloquent ORM included with Laravel is a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table. One of its key features is the option to write database queries in a proprietary object oriented SQL dialect called Eloquent Query Language (EQL). This provides developers with a powerful alternative to SQL that maintains flexibility without requiring unnecessary code duplication.
Defining Eloquent Model
To get started, let's create an Eloquent model. Models typically live in the app
directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json
file. All Eloquent models extend Illuminate\Database\Eloquent\Model
class.
The easiest way to create a model instance is using the make:model
Laravel Artisan command:
php artisan make:model Product
Eloquent Model Conventions
Now, let's look at an example Product
model, which we will use to retrieve and store information from our products
database table:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { // }
Table Names
Note that we did not tell Eloquent which table to use for our Product
model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Product
model stores records in the products
table. You may specify a custom table by defining a table
property on your model:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { /** * The table associated with the model. * * @var string */ protected $table = 'my_products'; }
Primary Keys
Eloquent will also assume that each table has a primary key column named id
. You may define a protected $primaryKey
property to override this convention.
In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int
automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing
property on your model to false
. If your primary key is not an integer, you should set the protected $keyType
property on your model to string
.
Timestamps
By default, Eloquent expects created_at
and updated_at
columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps
property on your model to false
:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { /** * Indicates if the model should be timestamped. * * @var bool */ public $timestamps = false; }
If you need to customize the format of your timestamps, set the $dateFormat
property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { /** * The storage format of the model's date columns. * * @var string */ protected $dateFormat = 'U'; }
If you need to customize the names of the columns used to store the timestamps, you may set the CREATED_AT
and UPDATED_AT
constants in your model:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { const CREATED_AT = 'creation_date'; const UPDATED_AT = 'last_update'; }
Database Connection
By default, all Eloquent models will use the default database connection configured for your application. If you would like to specify a different connection for the model, use the $connection
property:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { /** * The connection name for the model. * * @var string */ protected $connection = 'connection-name'; }
If you have any other questions, experience or insights on "Laravel Eloquent ORM" please feel free to leave your thoughts in the comments bellow which might be helpful to someone!
Be the first one to write a response :(
{{ reply.member.name }} - {{ reply.created_at_human_readable }}