To select specific columns from a database using Eloquent, you can use the select()
method.
Here’s an example:
1 2 3 |
$users = User::select('name', 'email')->get(); |
This will retrieve the name
and email
columns for all users from the database.
You can also use the select()
method with other query builder methods, such as where()
or orderBy()
. For example:
1 2 3 4 5 6 |
$users = User::select('name', 'email') ->where('age', '>', 25) ->orderBy('name', 'asc') ->get(); |
This will retrieve the name
and email
columns for all users who are older than 25, ordered by name
in ascending order.