Laravel Eloquent ORM or Object Relational Mapper is not just impressive, but expressive as well. If you are PHP programmer and have strong grip in working with “Objects” in PHP, you might be well aware of the utility of Eloquent! It is not easy, but undoubtedly, the most expressive syntax in PHP; especially when it comes to working with Models. Starting with the building blocks of this massive topic, programmers can learn to roll in. Eloquent ORM in Laravel has made it extremely effortless for programmers to interact with database.
Read More:- PHP framework developer
Eloquent ORM in Laravel: How Does It Work?
Eloquent ORM in Laravel offers an ActiveRecord implementation for database to work with, which means that every model created in MVC structure resembles to a table of the database. A “Spoon” model will resemble to a “spoons” table in the database. As, programmers has convention while creating database tables and models, he/she can easily implement or take data from database.
For example, in order to get all spoons from a Spoon model, all programmer has to do is type Spoon::all(). Bringing this function in use would help you fetch information from database and produce or create a proper SQL command.
Data base queries are no longer a night mare
Trust me! Data base queries are a night mare for all the developer as they promise to fetch you highly optimised search results from this colossal ocean of data. No worries! Eloquent ORM has optimised this by providing a smooth platform to deal with your queries. It offers a wide range easy to make database relations which are given below.
The elementary CRUD functions-:
- Ready to use one-to-one relationships
- Ready to use one-to-many relationships
- Ready to use many-to-many relationships
To commence with Eloquent, we need first set up the application and database.
Those first two parts are easy enough so get those done. Then we’ll work on migrating and seeding our database. For this you just need to
- First, Install Laravel: composer install –prefer-dist
- Modify your database settings in app/config/database.php
- Construct migrations via artisan
- Construct Eloquent models
- Seed your database
How to create Migrations
We know that we Migrations are the easiest way to add tables in the database. To get detailed information about it you can refer Laravel docs.
However, I am trying here to create a simple table named as lion.
We can easily create a migration by a command line type.
php artisan migrate:make create_fish_table –create=fish
We can use this field to specify the fields as mentioned below.
Schema::create(‘lion’, function(Blueprint $table)
$table->integer(‘danger_level’); // specifying age
Auto-increment id is included in these migrations. Furthermore, timestamps for the field updated_at and created_at. It automatically updates the record when it is updated. Similarly, we can create a migration named as Zoo and we can relate them using a pivot table. To define many-to-many relationship we need to write the following code
php artisan migrate:make create_lions_zoo_table –create=lions_zoo
Schema::create(‘lions_zoo’, function(Blueprint $table)
$table->integer(‘lions_id’); // id of the lion
$table->integer(‘zoo_id’); // the id of the zoo in which the bear is at
This is by far the simplest way to create many-to- many relationships.
Why Eloquent ORM?
Working over data acts as a crucial part of every web application. Taking in traditional terms, data of PHP web applications is stored in database table rows. With version 5.0, PHP language generated an integrated interface to fetch the results and query databases; while the other new features that added with proved useful to handful of basic applications. Moreover, they ended up making application’s code appear messy, where SQL statements seemed entwined with PHP code. Also, convenience turned out to be yet another factor that increased the quantity of effort while working with data utilizing foreign keys.
It is very obvious that data when stored in database table would help downsize the effort. If said in other words it would simply the effort made access to data from database, along with making possible to refer the data through assigning objects to different objects’ properties. This is what Active record pattern is all about; and Laravel implements this technique in “Eloquent ORM”.
As mentioned above, ORM endows programmers with the capability to access and manipulate data that is stored within a database, even when it is a PHP object. Eloquent ORM of Laravel enables developers to make of Active Record pattern to its fullest making the code of application that is used in conjunction with database look clean, not messy. Producing new database entries, deleting and manipulating, querying the database utilizing a variety of different parameters; in addition made readable and easy with Eloquent ORM. Eloquent is efficient of making assumptions about databases, but developers and programs can both override and customize those assumptions at the same time (convention over configuration).
For example, if developer has a table called “shoes” in the database, there must be a model naming “Shop” in order to correspond with the table. If the developer wants to utilize Eloquent ORM to produce a fresh row in “shoes” table comprising the information of a new shoe, he/she can do that by producing a new object of Shoe class and operate over that object. Best of all, developers are not limited over control and operating of table in database, built-in relationships grants developers with ability to manipulate related models as well as related tables.
Read More:- PHP development india