You can use the select
method in Laravel's Eloquent ORM to retrieve specific columns from a table. Here's how you can do it:
users
table, you would import the User
model:use App\Models\User;
select
method on the model to specify the columns you want to retrieve. Pass the column names as arguments to the select
method. If you want to retrieve multiple columns, separate them using a comma. For example, to retrieve the name
and email
columns from the users
table, you can write:$users = User::select('name', 'email')->get();
select
method will return a collection of models that match your query. You can then perform other operations on this collection, such as iterating over the results or applying additional conditions.foreach ($users as $user) {
echo $user->name; // Access the name column
echo $user->email; // Access the email column
}
Note: When using the select
method, Laravel will only retrieve the specified columns from the database, which can improve performance if you only need a subset of the data.