May Sale! All accounts are 40% off this week.

zaster's avatar

Eloquent methods and Collection methods

Are these the same? Can we use these methods in both Eloquent and Collections

0 likes
10 replies
tokoiwesley's avatar
Level 11

They are not the same, however Eloquent collections extend the base Laravel collection object, thus inheriting all the methods provided by the base collection.

When retrieving a record from a table using Eloquent ORM, the resulting object is an Eloquent collection. So you can use Laravel collection methods on Eloquent collections.

In addition, what you should note is that an Eloquent model is a query builder thus all the methods available in the Query Builder may be used in Eloquent queries and then the get() method is used to retrieve the results as an Eloquent collection.

$user = App\User::whereNotNull('email_verified_at')
    ->orderBy('id', 'desc')
    ->take(20)
    ->get();

EDIT:

When retrieving a record from a table using Eloquent ORM, the resulting object is an Eloquent collection. So you can use Laravel collection methods on Eloquent collections.

When retrieving a single record from a table using Eloquent ORM (first(), find(), firstOrFail() and findOrFail()) the resulting object is a model. However, all multi-result sets returned by Eloquent are Laravel collection objects - instances of Illuminate\Database\Eloquent\Collection, including results retrieved via the get() method or accessed via a relationship. So you can use Laravel collection methods on Eloquent collections.

Snapey's avatar

@tokoiwesley just check, I think this is wrong

When retrieving a record from a table using Eloquent ORM, the resulting object is an Eloquent collection. So you can use Laravel collection methods on Eloquent collections.

When retrieving a record from a table using Eloquent then what you get is a single model (eg, using find() or first() etc)

When retrieving multiple records then they are held as models in an Eloquent Collection (eg using get())

Sorry if I seem pedantic, but this is a common area for confusion when people do things like

$post = Post::where('id',1)->get();

and then cannot understand why they cannot access $post->title etc

1 like
jlrdw's avatar

However, if the resultset has tens of thousands of records, (why laravel folks say models?), with that many, a collection is totally useless.

You are probably going to use pagination anyway.

I still don't understand the need of "collections". Data can be retrieved and worked with via well written queries.

Yes, the many post and artices examples you see on the forum is great, but now picture a large comany with a huge receivables report.

One company may only have a few receivables, but another company might have several thousand receivables.

So a collection, I don't think so. Just learn how to write queries that gets your data.

WIth large resultsets expected, you probably need to apply some filtering anyway, i.e. via a from and to date.

So basically just saying some common sense has to come in on when to and when not to use collections. Meaning whether to put a whole resultset into a collection or not.

zaster's avatar

@tokoiwesley and @snapey

Thank you very much for your contribution

@jlrdw

Is it possible to do everything that a collection could, using eloquent ? It doesn't matter if it is a longer way of getting things done

jlrdw's avatar

Is it possible to do everything that a collection could, using eloquent ?

For the most part yes. But eloquent has limitations, there are times where regular sql needs to be used.

You do not want a hundred thousand items in a collection, a collection is fine for smaller results, like a few hundred or less.

So normal sql, the answer is yes. Eloquent, mostly.

A while back a question came up on getting a maxid, here is example I gave.

$quy = Powner::query()->leftJoin('dc_pets', 'dc_powners.ownerid', '=', 'dc_pets.ownerid')
                ->select('dc_powners.ownerid', 'dc_powners.oname')->distinct()
                ->selectRaw('max(dc_pets.petid) as maxPetId')
                ->where('dc_powners.ownerid', '<', 4)
                ->groupby('dc_powners.ownerid')
                ->orderby('dc_powners.oname')
                ->get();

        foreach ($quy as $r) {
            echo $r->oname . "  ---   " . $r->maxPetId . "<br>";
        }

results:

DIANE --- 77
JEFFERY --- 6
JIMMIE --- 92

Just test database and test data. And just a quick example I did to answer a question.

So you can query just about anything with eloquent and query builder techniques, but remember some trial and error is required to work out the correct query.

As example see here, even with help, still took time to work out:

https://laracasts.com/discuss/channels/eloquent/orderby-computed-related-attribute

Like @tokoiwesley stated, eloquent has all of query builders methods.

And relationships can be used also for many things, but not everything.

Snapey's avatar

if the resultset has tens of thousands of records, ... a collection is totally useless.

You are probably going to use pagination anyway.

What utter tripe. Who paginates tens of thousands of records? If you are dealing with datasets that large you find better methods of getting to the data you need rather than splashing it to the page (or pages)

The OP asked what the difference was. A complete ramble in a totally other direction is not helpful.

jlrdw's avatar

What utter tripe. Who paginates tens of thousands of records?

I went to google and searched laravel, got paginated results of:

About 20,800,000 results

Sorry it sounds like you are rambling.

Also:

The OP asked what the difference was.

I answered, YOU DON'T PUT LARGE RESULTS IN A COLLECTION IS THE DIFFERENCE. Is the jest of what I was saying, which is a difference.

Did that square you away.

@snapey

because the architecture paradigm is called MVC

I call a database record a database record, if you want to call it a model oh well.

For others reading this, sorry it got a little nasty, but my intentions was meant well.

Meaning there are some folks new to the framework that may not realize that it's not good to put too much data in a collection.

Naturally I looked at that as a difference. Because you can have quite a lot of results in a query and paginate if needed.

Please or to participate in this conversation.