Skip to main content
Eloquent - ORM in Laravel

Eloquent – ORM in Laravel

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('App\Models\Address',’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('App\Models\XXX,’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.

 

 

 

 

Albert S

Albert S

Albert works as PHP Developer with Trigent Software. He has nearly 6 years of experience in PHP/MVC technology. He also has strong experience in Laravel, CodeIgniter, ZF1 Framework,MySql,JQuery and Java script.

5 thoughts on “Eloquent – ORM in Laravel

  1. I was curious if you ever considered changing the structure of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or 2 pictures. Maybe you could space it out better?|

  2. Hey! Would you mind if I share your blog with my myspace group? There’s a lot of people that I think would really enjoy your content. Please let me know. Cheers|

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 42 = fifty