Home / Web Development Technologies / Laravel / Laravel’s Eloquent ORM versus Query Builder
Eloquent ORM versus Query Builder
Eloquent ORM versus Query Builder

Laravel’s Eloquent ORM versus Query Builder

Laravel’s Eloquent ORM versus Query Builder




Laravel offers two approaches to work with your database:

Eloquent ORM: Eloquent gives a active record-based ORM. It furnishes a familiar interface with built-in relational capacities.

Query Builder The Query Builder gives an adaptable, furthermore familiar, approach to make SQL queries. It gives a natural approach to make SQL queries with about the greater part of the adaptability of utilizing SQL direct.

Advancing through learning Laravel, a large portion of the videos and articles I read concentrated on utilizing Eloquent for database access. On the other hand, I observed that utilizing Eloquent for producing JSON, at least for some tasks, constrained the control I had over how come about set JSON was generated. This article indicates how I swung to the Query Builder for producing the outcome sets to be communicated as JSON.

As I advance further into Laravel, I am much sure there will undertakings for which Eloquent is magnificently suited. Notwithstanding, I realized this weekend there are additionally tasks for which the Query Builder is greatly suited.

Now let’s explore Eloquent ORM versus Query Builder one by one.

Utilizing Eloquent ORM

This Eloquent code (expecting the comparing Laravel relations have been built up).

$tasks = Project::with( ['category', 'state', 'type'] )->
           where('status_id', '<>', 3)->
           orderBy('description')->
           orderBy('id', 'desc')->get(['projects.id',
                                       'projects.type_id',
                                       'projects.status_id',
                                       'projects.category_id',
                                       'projects.description',
                                       'projects.start_time']);
return $projects;

generates this JSON:

[
    {
        id: "2",
        type_id: "2",
        status_id: "1",
        category_id: "1",
        description: "Dr Fahad Qureshi",
        start_time: "2013-08-04 16:00:00",
        category: {
            id: "1",
            text: "Personal",
            created_at: "2013-07-17 04:47:37",
            updated_at: "2013-07-17 04:47:37"
        },
        state: {
            id: "1",
            text: "Open",
            created_at: "2013-07-17 04:47:37",
            updated_at: "2013-07-17 04:47:37"
        },
        type: {
            id: "2",
            text: "Appointment",
            created_at: "2013-07-17 04:47:37",
            updated_at: "2013-07-17 04:47:37"
        }
    },
    ...
]

See how the information from the related tables is put into seperate JSON nodes (ie, the class, state, and type nodes). Unless you utilize the $hidden exhibit in a model definition, there isn’t an approach to restrict the columns came back from related tables. The $hidden array can’t be changed at runtime so it must be indicated in the model. Subsequently you either require special-cased models or figure out how to live with the greater part of the columns being returned for related tables.

Laravel’s Query Builder to the salvage

Utilizing Laravel’s Query Builder, you can get considerably less verbose JSON. The following query:

$tasks = DB::table('projects')
         ->join('projects_categories', 'projects.category_id', '=', 'projects_categories.id')
         ->join('projects_states'    , 'projects.status_id'  , '=', 'projects_states.id')
         ->join('projects_types'     , 'projects.type_id'    , '=', 'projects_types.id')
         ->orderBy('projects.description')
         ->orderBy('projects.id', 'desc')
         ->where('status_id', '<>', 13)
         ->select('projects.id',
                  'projects.status_id',
                  'projects.notes',
                  'projects.start_time',
                  'projects.end_time',
                  'projects.labels',
                  'projects.description', 
                  'projects_categories.text as category',
                  'projects_states.text as status',
                  'projects_types.text as type')
         ->get();

return $tasks;

produces this JSON. The Query Builder gives a more prominent level of control over what is incorporated into the query. For instance, take note of how you can utilize the “as” condition in the select() method to assumed alias result segments.

[
    {
        id: "2",
        status_id: "1",
        notes: "check-up",
        start_time: "2013-08-04 16:00:00",
        end_time: "2013-08-04 17:00:00",
        labels: "",
        description: "Dr Schnieder",
        category: "Personal",
        status: "Open",
        type: "Appointment"
     },
    ...
]

Having any question or suggestion regarding Laravel’s Eloquent ORM versus Query Builder , please feedback us using comment box.

Laravel’s Eloquent ORM versus Query Builder Laravel offers two approaches to work with your database: Eloquent ORM: Eloquent gives a active record-based ORM. It furnishes a familiar interface with built-in relational capacities. Query Builder The Query Builder gives an adaptable, furthermore familiar, approach to make SQL queries. It gives a natural approach to make SQL queries with about the greater part of the adaptability of utilizing SQL direct. Advancing through learning Laravel, a large portion of the videos and articles I read concentrated on utilizing Eloquent for database access. On the other hand, I observed that utilizing Eloquent for producing…

0%

User Rating: 4.47 ( 5 votes)

Check Also

AngularJS and Laravel Blade

How to use AngularJS and Laravel Blade together

How to use AngularJS and Laravel Blade together In this article I will show you …

Ajax Pagination Using Laravel

How to Create an Ajax Pagination Using Laravel

In this article I will demonstrate one way to create an Ajax pagionation using Laravel …

2 comments

  1. Great post. I was checking constantly this blog and I’m impressed!
    Very helpful information particularly the last part 🙂
    I care for such information much. I was looking for this certain information for a long time.
    Thank you and good luck.

  2. Great job man, It really helps me too much, keep it up, we are looking for more interesting articles.
    It would be better if you could write an article about Eloquent Relationships. thanks

Leave a Reply

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