Calling query builder methods from an eloquent model instance

morbidCode

I am very new in Laravel and active records in general. I apologize in advance if this is a stupid question. But I would like to make sure if what I'm thinking is correct.

background:

The laravel docs says in the eloquent ORM section:

Since Eloquent models are query builders, you should review all of the methods available on the query builder. You may use any of these methods in your Eloquent queries.

It has been mention many times that eloquent queries aren't very good when processing very large data, and it is usually a good idea to use the base query builder instead.

I am wondering about the statement:

You may use any of these methods in your Eloquent queries.

Many examples I've read separates the two (E.G.

DB::table('table')->get();

usually faster than

model::all()

) in comparing performance.

I am planning to create my new project with Laravel, and am thinking about using both functionalities (the base query builder methods for processing complicated and large data then using specific eloquent methods for anything else).

My question:

I'm thinking of how I can make my code readable, clean, and reasonably efficient. I would be dealing with possibly large data (E.G. thousands to millions of rows and 20 to 30 tables on two databases). Is it safe to say that:

DB::table('table')->get();

is the same as:

MODEL::get();

and assuming that both only return a collection, in large data usually faster than

MODEL::all();

? Thanks.

rexw

It's important to look at the differences of the return from each statement. While both will return a collection, it's the items in the collection that make the difference.

DB::table('table')->get()

Returns a collection of stdClass objects with no additional processing done by Laravel. If you are using SoftDeletes, this will ignore that functionality. It will also return all fields in the row.

MODEL::get()

This returns a collection of MODELs. Laravel will load each result into a new instance of MODEL. Traits like SoftDeletes and properties like hidden[] and visible[] will be respected.

Depending on what exactly you are going to be doing with the data will probably be the deciding factor. If you're returning it in a response, you'll probably want to use the MODEL approach so traits and properties are respected. If you're processing the data returned for some other use, then the DB approach may be best.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From PHP

Persisting Eloquent Builder Instance in Laravel Livewire

From Dev

Why doesn't Eloquent\Builder inherit from Query\Builder in laravel?

From Dev

Get instance of Eloquent model from generic DB query in Laravel 5.1

From Dev

Instance the query builder directly from model

From Dev

Convert Eloquent Builder to Query Builder

From Dev

Eloquent Query Scope return Builder instead of Model when using phpunit

From Dev

How to create a Eloquent model instance from a raw Object?

From Dev

How to combine results of two query from Eloquent Model in Laravel?

From Mysql

Converting mysql query to Eloquent query builder

From Dev

Blazor JS interop - calling C# instance methods from javascript

From Dev

Property does not exist on the Eloquent builder instance

From Dev

Convert Query Builder to Eloquent Builder

From Dev

Eloquent model is returning as builder

From Dev

Laravel - What's the benefit of calling query() method on an Eloquent model

From Dev

Property [id] does not exist on the Eloquent builder instance

From Dev

query builder equivalent of Model::query();

From Dev

laravel eloquent relationship from query builder

From Dev

Calling instance methods?

From Dev

firstOrFail returning the wrong record from Eloquent Query Builder

From Dev

Eloquent Query Builder in laravel 5

From Dev

Create Eloquent model from complex raw SQL-query

From Dev

Adding methods to Eloquent Model in Laravel

From Dev

Laravel Query Builder to Eloquent Builder

From Dev

Model instance not returning methods

From Dev

Query builder with Laravel using Eloquent

From Dev

No query results for model [App\User] NULL 387 /laravel/framework/src/Illuminate/Database/Eloquent/Builder.php

From Dev

How to convert this into Query builder or eloquent?

From Dev

Laravel 8: Can I make Eloquent scope but NOT against query builder but against related model?

From Dev

Add field from query data to an Eloquent model without using an Attribute

Related Related

  1. 1

    Persisting Eloquent Builder Instance in Laravel Livewire

  2. 2

    Why doesn't Eloquent\Builder inherit from Query\Builder in laravel?

  3. 3

    Get instance of Eloquent model from generic DB query in Laravel 5.1

  4. 4

    Instance the query builder directly from model

  5. 5

    Convert Eloquent Builder to Query Builder

  6. 6

    Eloquent Query Scope return Builder instead of Model when using phpunit

  7. 7

    How to create a Eloquent model instance from a raw Object?

  8. 8

    How to combine results of two query from Eloquent Model in Laravel?

  9. 9

    Converting mysql query to Eloquent query builder

  10. 10

    Blazor JS interop - calling C# instance methods from javascript

  11. 11

    Property does not exist on the Eloquent builder instance

  12. 12

    Convert Query Builder to Eloquent Builder

  13. 13

    Eloquent model is returning as builder

  14. 14

    Laravel - What's the benefit of calling query() method on an Eloquent model

  15. 15

    Property [id] does not exist on the Eloquent builder instance

  16. 16

    query builder equivalent of Model::query();

  17. 17

    laravel eloquent relationship from query builder

  18. 18

    Calling instance methods?

  19. 19

    firstOrFail returning the wrong record from Eloquent Query Builder

  20. 20

    Eloquent Query Builder in laravel 5

  21. 21

    Create Eloquent model from complex raw SQL-query

  22. 22

    Adding methods to Eloquent Model in Laravel

  23. 23

    Laravel Query Builder to Eloquent Builder

  24. 24

    Model instance not returning methods

  25. 25

    Query builder with Laravel using Eloquent

  26. 26

    No query results for model [App\User] NULL 387 /laravel/framework/src/Illuminate/Database/Eloquent/Builder.php

  27. 27

    How to convert this into Query builder or eloquent?

  28. 28

    Laravel 8: Can I make Eloquent scope but NOT against query builder but against related model?

  29. 29

    Add field from query data to an Eloquent model without using an Attribute

HotTag

Archive