All methods available on the query builder are also available when querying Eloquent models.
User::select('name', 'email')->find(1);
User::where('id', '=', 1)->first(array('name'));
User::where('id', '=', 1)->pluck('name');
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();
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community