×


An introduction to Eloquent ORM in Laravel

ORM refers to Object Relational Mapping.What ORM tools like  Eloquent does is that they are equipped with a metadata descriptor which helps in connecting object code to a relational database.To learn more on ORM refer this article. https://truetocode.com/orm-using-sequelize-orm-with-express-node-js/418/

Now,we know that sequelize is an ORM tool that is used to connect node applications to relational databases such as Mysql/postgres.For PHP Developers ,Laravel is equipped with a very simple,efficient ORM called Eloquent ORM.

Using Eloquent

To create model class we use the following command

Now in case you dont know,Artisan is a command line interface provided with Laravel just like the other command line interfaces you might have used,like angular-cli,Artisan helps you to generate files while working with Laravel. The above command creates a model file for our Table and it looks like this

As you can see all models extend “Illuminate\Database\Eloquent\Model

Also note that the table has ‘snake case plural’ name of the class.That is ‘televisions’ table and ‘television’ class.Uncomment the $table variable in the above code to use custom table name.
Now,To generate a migration script on creating the model.This can be done using the following command.

The migration file looks like this

To run the migration,use

which runs all pending migrations.
This generates a table Television in our databse provided the details you provided inthe database configuration file is correct.The table will have an id (Auto incremented-primary) and a created_at field and updated_at field.

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:

Now we want to add some fields to the newly created Television table.You can either add the fields while creating the table as shown previously or create a new migration script .Here we create a new migration script

This is the new migration script generated . We add the following code to the migration script for adding some fields

 

running php artisan migrate will generate the following table

 

 

To generate migration to create a table after you have created the model , use php artisan make:migration newtablemigs –create=newtables

Now to consider we want to select all data from the database.There are two ways to achieve this.Querying can be done using the query builder or using Eloquent.

To use query builder

add use Illuminate\Support\Facades\DB as DB in your controller

or add this in your app.php file

 

Now build the query using the query builder

To use Eloquent,

This is just a quick overview .See the references below to have a detailed look in to various functionalities provided by Eloquent and for understanding more about query builder

Facades –       https://laravel.com/docs/5.6/facade

Querbuilder   –   https://laravel.com/docs/5.6/queries

Eloquent    –    https://laravel.com/docs/5.0/eloquent

 

One Comment

  • aaron stampler July 6, 2018 at 6:57 am

    Thanks for the tutorial

Leave a Reply

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