Support the ongoing development of Laravel.io →
Database Eloquent Views
Last updated 2 years ago.
0

All methods available on the query builder are also available when querying Eloquent models.

User::select('name', 'email')->find(1);
Last updated 2 years ago.
0
User::where('id', '=', 1)->first(array('name'));

User::where('id', '=', 1)->pluck('name');
Last updated 2 years ago.
0

Ok, so.. Here's the thing..

When using an ORM you're mapping a record to behavior in the form of the entity class. In this case, your entity is User and you're mapping to a record in the users table.

If you return a partial record, then you're actually running the risk of entirely disrupting the way that the User class operates. The behavior of an ORM object is not designed to handle partial datasets.

Even if it's fine in your current code, you're creating technical debt that will have to be fixed later.

If you want to just access the data, then do so with DB::table('users')->...->...->first();

Last updated 2 years ago.
0

ShawnMcCool said:

Ok, so.. Here's the thing..

When using an ORM you're mapping a record to behavior in the form of the entity class. In this case, your entity is User and you're mapping to a record in the users table.

If you return a partial record, then you're actually running the risk of entirely disrupting the way that the User class operates. The behavior of an ORM object is not designed to handle partial datasets.

Even if it's fine in your current code, you're creating technical debt that will have to be fixed later.

If you want to just access the data, then do so with DB::table('users')->...->...->first();

If you're already instantiating the user object anyway, are there any real performance gains from just selecting two attributes instead of a dozen? I imagine not, especially if the DB is on the same server as your website.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.