Analytics Made Easy - StatCounter

Fill the form & Get Installation Support


Laravel Eloquent

What is Eloquent ?

The Eloquent ORM encompasses with laravel which provides simple Active Record Implementation working which is working with the database. Database tables has related Models which is used to interconnect with that table. Database configuration exists in config/database.php, you can change according to your database details you are using in your project. First you can do database connection in .env file because database.php file using the configuration variables of .env file. Eloquent model given you the rights to insert, update & delete records from table as well. The benefit of eloquent models is, while performing any operation like listing with multiple relationship or creating record or updating record , instead of writing lengthy queries , use models to interact directly with tables.

Few of scenario’s have to follow according to laravel standard

Having 3 tables users, products & product_images table where productId is foreign key of product table.

Table : users

Table : products

Table : product_images

1. How to do create record operation  :

Instead of following syntax :

Do this :

2. How to do fetch record operation  :

Instead of following syntax :

Do this :

Define relationship between models 

Here we are having 3 models i.e. User model, Product model & Product Images model.

Eloquent query including above 3 tables

In simplified way instead of join clause you can use relationship & get the detail according to the requirements.

If the requirement is to get details with pagination , can use the query like given below :

About the above query , sometimes due to some clause like ‘HAVING’ in laravel eloquent query paginate clause is not working fine, in that situation can use simplePaginate(10). Here paginate(10) defines, you will get 10 records per page. CAST is using here in userDetail relationship to convert integer to string.

Leave a Reply

Your email address will not be published.

Fill the below form if you have any Laravel programming need.

Let’s get started!