---
title: "Laravel 13: How to Use Query Scopes for Cleaner Queries"
url: "https://magecomp.com/blog/laravel-13-use-query-scopes/"
date: "2026-04-27T12:09:50+00:00"
modified: "2026-04-27T12:09:52+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "Laravel"
word_count: 758
reading_time: "4 min read"
summary: "If you've ever found yourself copy-pasting the same where() conditions across multiple Eloquent queries in your Laravel application, you're not alone, and there's a better way. Laravel 13 query sco..."
description: "Learn how to use Laravel 13 query scopes to write cleaner, reusable Eloquent queries."
keywords: "Laravel"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Laravel 9: New Database Commands You Must Know"
    url: "https://magecomp.com/blog/laravel-9-new-database-commands/"
  - title: "How to Implement API Versioning in Laravel"
    url: "https://magecomp.com/blog/implement-api-versioning-laravel/"
  - title: "How to Integrate Google reCAPTCHA with Laravel?"
    url: "https://magecomp.com/blog/integrate-google-recaptcha-with-laravel/"
---

# Laravel 13: How to Use Query Scopes for Cleaner Queries

_Published: April 27, 2026_  
_Author: Bharat Desai_  

![Laravel 13 How to Use Query Scopes for Cleaner Queries](https://magecomp.com/blog/wp-content/uploads/2026/04/Laravel-13-How-to-Use-Query-Scopes-for-Cleaner-Queries-1024x512.webp)

If you’ve ever found yourself copy-pasting the same where() conditions across multiple Eloquent queries in your Laravel application, you’re not alone, and there’s a better way. Laravel 13 query scopes let you encapsulate your query logic into reusable, readable methods, keeping your codebase clean and DRY (Don’t Repeat Yourself).

In this guide, we’ll walk you through everything you need to know about query scopes in Laravel 13.

[![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/)**Prerequisite:**

- Composer (latest Version)
- Laravel version 13

## Steps to Use Query Scopes in Laravel 13:
Here are the steps to follow:

Step 1: Install Laravel 13

Step 2: Set Database details and migrate

Step 3: Create Model and Migration

Step 4: Create Routes

Step 5: Create Controller

Step 6: Create Blade File

Step 7: Test Project

Now, let’s see all the steps with the detailed information.

### Step 1: Install Laravel 13
We need a new project for this demo. Create it using the command below:

```
composer create-project laravel/laravel:^13.0 query-scope-demo
```

### Step 2: Set Database details and migrate
Set the database details in the .env as your current credentials and use the below command to migrate the database.

```
php artisan migrate
```

### Step 3: Create Model and Migration
Now, create a Product model with migration.

```
php artisan make:model Product -m
```

Update migration file:

**database/migrations/create_products_table.php**

```
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('price');
            $table->string('status')->default('active');
            $table->timestamps();
        });
    }
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};
```

Now run migration:

```
php artisan migrate
```

Update Product model with Query Scope:

**app/Models/Product.php**

```
<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Product extends Model
{
    protected $fillable = [
        'name',
        'price',
        'status'
    ];
    /**
     * Scope for active products
     */
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }
}
```

### Step 4: Create Routes
Now, create a route for displaying active products

**routes/web.php**

```
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersProductController;
Route::get('/', [ProductController::class, 'index']);
```

### Step 5: Create Controller
Here, we will create ProductController. Use the command below to create the controller.

```
php artisan make:controller ProductController
```

**app/Http/Controllers/ProductController.php**

```
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsProduct;
class ProductController extends Controller
{
    /**
     * Display active products using query scope
     */
    public function index()
    {
        $products = Product::active()->get();
        return view('products', compact('products'));
    }
}
```

### Step 6: Create Blade File
Now, create blade file

**resources/views/products.blade.php**

```

<html>
<head>
    <title>Laravel Query Scope Example</title>
</head>
<body>
<h2>Active Products List</h2>
<table border="1" cellpadding="10">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Price</th>
        <th>Status</th>
    </tr>
    @foreach($products as $product)
    <tr>
        <td>{{ $product->id }}</td>
        <td>{{ $product->name }}</td>
        <td>{{ $product->price }}</td>
        <td>{{ $product->status }}</td>
    </tr>
    @endforeach
</table>
</body>
</html>
```

### Step 7: Test Project
Now, run the Laravel app:

```
php artisan serve
```

Now, go to your web browser, type the given URL, and view the app output:

http://localhost:8000/

## Conclusion
Laravel 13 provides query scopes that tidy up Eloquent queries without extra clutter. Since these let you reuse common filters, think of them like shortcuts baked right into models. Instead of repeating conditions everywhere, wrap them inside a scope method. When inputs change often, reach for ones built to accept arguments on the fly. Filters applied every time? Global versions run automatically across all queries. One after another, they stack neatly – no messy joins or tangled WHERE blocks. Because each has a name, reading code feels more like following steps. Over time, this keeps rules grouped where they belong. Even complex checks become easier to scan later. So long as they stay small, maintenance never gets out of hand.

[![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/)

## FAQ
**1. What is the difference between local and global scopes in Laravel?**

Some settings work only where you decide, others kick in every time by default across each request for that data type.

**2. Are query scopes only available in Eloquent?**

True, you’ll find query scopes living inside Laravel’s Eloquent ORM – they don’t show up when using the Query Builder on its own. Instead, that tool works without them, sticking to a different style altogether.

**3. Do query scopes affect performance?**

No, query scopes do not negatively impact performance. They simply organize your query logic and are executed as normal SQL queries.

</body></html>


---

_View the original post at: [https://magecomp.com/blog/laravel-13-use-query-scopes/](https://magecomp.com/blog/laravel-13-use-query-scopes/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-04-27 13:06:29 UTC_  
