---
title: "Optimize Queries using select() in Laravel Eloquent"
url: "https://magecomp.com/blog/optimize-queries-using-select-in-laravel-eloquent/"
date: "2026-04-17T12:56:32+00:00"
modified: "2026-04-17T12:56:33+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "Laravel"
word_count: 363
reading_time: "2 min read"
summary: "When working with Laravel, many developers simply fetch data using all() or get() without thinking much about it."
description: "Learn how to optimize database queries in Laravel Eloquent using the select() method."
keywords: "Laravel"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Testing All Routes in Laravel with Pest"
    url: "https://magecomp.com/blog/test-all-routes-in-laravel-with-pest/"
  - title: "Display Limited Records Per Page Using Laravel Pagination"
    url: "https://magecomp.com/blog/display-limited-records-per-page-laravel-pagination/"
  - title: "How to Create PDF File in Laravel 8?"
    url: "https://magecomp.com/blog/create-pdf-file-laravel-8/"
---

# Optimize Queries using select() in Laravel Eloquent

_Published: April 17, 2026_  
_Author: Bharat Desai_  

![Optimize Queries using select() in Laravel Eloquent](https://magecomp.com/blog/wp-content/uploads/2026/04/Optimize-Queries-using-select-in-Laravel-Eloquent-1024x512.webp)

When working with Laravel, many developers simply fetch data using all() or get() without thinking much about it.

So instead of doing that, Laravel gives us a simple method called select(). In this blog, we’ll understand how to use select() with a simple example.

[![Laravel Development Services](https://magecomp.com/blog/wp-content/uploads/2024/12/Laravel-Development-Services-4-1024x284.webp)](https://magecomp.com/services/laravel-development-services/)

## Prerequirement:
- Composer (latest version)
- Laravel version 12

## Steps to Optimize Queries using select() in Laravel Eloquent:
1. Create a new Laravel project
2. Create migration and model
3. Add dummy data
4. Create controller
5. Use the select() method
6. Test the output

### Step 1: Create a New Laravel Project
```
composer create-project laravel/laravel select-demo
```

Move into the project:

```
cd select-demo
php artisan serve
```

### Step 2: Create Model and Migration
```
php artisan make:model Product -m
```

Now open the migration file and update it:

```
public function up(): void
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->text('description');
        $table->decimal('price', 10, 2);
        $table->integer('stock');
        $table->timestamps();
    });
}
```

Run migration:

```
php artisan migrate
```

### Step 3 : Add Dummy Data
Instead of adding data manually, we can use a Seeder

Run this command:

```
php artisan make:seeder ProductSeeder
```

Now open,

**database/seeders/ProductSeeder.php**

and edit as below,

```
public function run(): void
{
 Product::create([
 'name' => 'Laptop',
 'description' => 'Gaming Laptop',
 'price' => 75000,
 'stock' => 5
 ]);
Product::create([
 'name' => 'Mobile',
 'description' => 'Smartphone',
 'price' => 20000,
 'stock' => 10
 ]);
}
```

### Step 4 : Create Controller
```
php artisan make:controller ProductController
```

### Step 5 : Use the select() Method
```
use AppModelsProduct;
public function index()
{
    $products = Product::select('id', 'name', 'price')->get();
    return response()->json($products);
}
```

### Step 6 : Add Route
```
use AppHttpControllersProductController;
Route::get('/products', [ProductController::class, 'index']);
```

### Step 7 : Test the Output
Now open your browser and hit the URL below,

http://127.0.0.1:8000/products

And after hitting the url, you will get the output as below,

```
[
  {
    "id": 1,
    "name": "Laptop",
    "price": "75000.00"
  }
]
```

## Conclusion:
The select() method is simple, but very useful when you want to optimize your queries.

Instead of fetching everything, just get what you need.

[![Hire laravel Developer](https://magecomp.com/blog/wp-content/uploads/2024/12/Hire-Laravel-Expert-Now-3-1024x284.webp)](https://magecomp.com/services/hire-laravel-developer/)

## FAQ
**1. What is Laravel Eloquent?**

Laravel Eloquent is an Object-Relational Mapping (ORM) system that allows developers to interact with the database using PHP models instead of writing raw SQL queries.

**2. Why is query optimization important in Laravel?**

Query optimization improves application performance by reducing database load, speeding up data retrieval, and minimizing server resource usage.


---

_View the original post at: [https://magecomp.com/blog/optimize-queries-using-select-in-laravel-eloquent/](https://magecomp.com/blog/optimize-queries-using-select-in-laravel-eloquent/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-04-17 12:56:34 UTC_  
