Eloquent is an ORM framework which is used in Laravel to make web development easier and faster by focusing on object-oriented approach rather than writing plain SQL queries. Also, by defining the relationship in the model classes, we could avoid the lengthier join queries in the controllers. Mappable is an awesome feature which is an added advantage to this, supported by Eloquence.
Lets divide the topic into four parts
- Class creation
- Define the table properties
- Define the relationships
- Mappable
php artisan model::Tablename
Define the table properties:
Lets create the class ‘User’, here Eloquent assumes the table name as ‘users’. Oh No, the table name is having different signature, nothing to worry eloquent gives an option to override the same.
Class creation:
Each table should have the PHP class file. Artisan command helps to create the file
php artisan make : model ‘user’
protected $table = ‘user’;
Eloquent assumes the table’s primary key is ‘id’ and its data type is ‘integer’. In case of the primary key field is different than we have to override this
protected $primaryKey = ‘userid’;
Incase the primary key is not an auto increment !
public $incrementing = false;
Eloquent assumes the table has ‘created_at’ and ‘updated_at’ date fields. These two fields values are managed by itself, hence no need of sending values to them manually.
While insertion the ‘created_at’ field and while update the ‘updated_at’ field gets inserted. When the table does not have the fields, lets make it false to ignore.
public $timestamps = false;
Some times we may have the table fields with different names In such a case define as below
const CREATED_AT = 'CreatedAt';
const UPDATED_AT = ‘UpdatedAt’;
And finally we can restrict the fields which are whitelisted and blacklisted.
Whitelist fields can be mentioned as below
protected $fillable= [‘column1’,’column2’];
When we have more fields to be mentioned in the fillable, its tedious right. In such cases, we can use guarded instead of fillable.
Blacklist fields can be mentioned as below
protected $guarded = [‘id’];
I prefer to use guarded only, in most of cases the primary key and the fields which should not inserted / updated would go here.
Define the relationships:
We can define the relationship in the model class itself.
Let’s assume the user’s address has been stored in address tables. How the relationship needs to be defined, here you go
In a user model class :
public function address() { return $this->hasOne('AppModelsAddress',’user_id’,’id’); }
*Address is the other model class for the address table.
Id => primary key
user_id => foreign key reference
Well, the other table might have more than one record. How to implement that?
return $this->hasMany('AppModelsXXX,’foreign_key’,’primary_key’);
How to obtain the address data in user controller
$user= User::with('address')->get();
Mappable:
Consider a table contains the following field names.
ORDER1
FIRST_NAME
attach
Contact_number
Absolutely this works fine when we write the queries of Create, Read, Update and Delete (CRUD). We just want to use the same database fields in different name.
Sample code :
Good field names => bad field names $maps = [ ‘order’ => ‘ORDER’, ‘first_name’ => ‘FIRST_NAME’, ‘attachment’ => ‘attach’, ‘contact_number’ => ‘Contact_number’ ];
This is how we can maintain the field names across the application.