Optimize Queries using select() in Laravel Eloquent

Optimize Queries using select() in Laravel Eloquent

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

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 App\Models\Product;
public function index()
{
    $products = Product::select('id', 'name', 'price')->get();
    return response()->json($products);
}

Step 6 : Add Route

use App\Http\Controllers\ProductController;
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

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.

Previous Article

Fortnite: Virtual Goods and the Evolution of E-commerce

Next Article

KOR vs Avada vs MinMaxify: Which Order Limits App is Right for your Store?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨