PHPackages                             hamidreza-mozhdeh/filter-by-query-string - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. hamidreza-mozhdeh/filter-by-query-string

ActiveLibrary[Database &amp; ORM](/categories/database)

hamidreza-mozhdeh/filter-by-query-string
========================================

Filter Laravel Eloquent results by URL query strings and Eloqunet's scopes.

v1.0.3(2y ago)471[1 PRs](https://github.com/hamidreza-mozhdeh/filter-by-query-string/pulls)PHPPHP &gt;=8.0

Since Dec 15Pushed 2y ago1 watchersCompare

[ Source](https://github.com/hamidreza-mozhdeh/filter-by-query-string)[ Packagist](https://packagist.org/packages/hamidreza-mozhdeh/filter-by-query-string)[ RSS](/packages/hamidreza-mozhdeh-filter-by-query-string/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

Make Laravel Eloquent Filterable by URL query strings
=====================================================

[](#make-laravel-eloquent-filterable-by-url-query-strings)

Please give me a star if you like it.
-------------------------------------

[](#please-give-me-a-star-if-you-like-it)

About Filter by query string
----------------------------

[](#about-filter-by-query-string)

With this package, you can simply use Eloquent scopes to filter the model result by using URL query strings. The **advantage** of this repository is that you can **reuse your local scopes** in your business logic (Controllers, Services, etc).

- It's safe
- Reusable local scopes
- Easy to implement
- Customizable

How to install
--------------

[](#how-to-install)

Run `composer require hamidreza-mozhdeh/filter-by-query-string`. It will add a `FilterByQueryString` trait to your project.

How to use
----------

[](#how-to-use)

- Add the trait to your models `use FilterByQueryString;`.

### Category Model:

[](#category-model)

```
  class Category extends Model
  {
    use CategoryScopesTrait;
    use FilterByQueryString;
  }

```

- In your controller simply pass the Form Request to the Model:
    - Example: `$categories = Category::filter($request);`
    - Please note you have to type hint the `Request` class.
    - #### Important: First of all is better to have a From Request for each of your actions or methods (optional). If not, you have to change `requestMethod` to `input` which is not recommended.

        [](#important-first-of-all-is-better-to-have-a-from-request-for-each-of-your-actions-or-methods-optional-if-not-you-have-to-change-requestmethod-to-input-which-is-not-recommended)

### CategoryController:

[](#categorycontroller)

```
class CategoryController extends Controller
{
    public function index(CategoryRequest $request)
    {
        return Category::filter($request)->get();
    }

```

### CategoryRequest:

[](#categoryrequest)

```
class CategoryRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => ['sometimes', 'string', 'min:3', 'max:254'], // A single filter

            // Use Associative example
            'date-between' => ['sometimes', 'array'], // A filter with two values
            'date-between.from' => ['sometimes', 'date'], // Associative array
            'date-between.to' => ['sometimes', 'date'], // Associative array

            // Or Indexed example
            'date-between' => ['sometimes', 'array'], // A filter with two values
            'date-between.*' => ['sometimes', 'date'], // Indexed array
        ];
    }
}

```

- Define your scopes to use the filters
    - It's better to have a directory and traits for your models in some where like `App\Models\Traits\Scopes\CategoryScopesTrait`.

### CategoryScopesTrait:

[](#categoryscopestrait)

```
trait CategoryScopesTrait
{
    public function scopeName(Builder $query, string $name): Builder
    {
        return $query->where('name', 'like', "%{$name}%");
    }

    public function scopeDateBetween(Builder $query, string $from, string $to): Builder
    {
        return $query->whereBetween('created_at', [$from, $to]);
    }
}

```

- To filter the categories' names for example `ossw` use:
    `http://0.0.0.0/api/categories?name=ossw`
- To filter with dates use:
    Indexed array: `http://0.0.0.0/api/categories?date-between[]=2023-11-23&date-between[]=2023-11-25`
    Associative array: `http://0.0.0.0/api/categories?date-between[from]=2023-11-23&date-between[to]=2023-11-25`

Customization
-------------

[](#customization)

```
  $categories = Category::filter(
    request: $request,
    only: ['name'], // Only accept this filter.
    except: ['date-between'], // Do not accept the `date-between` method.
    prefix: 'filters', // Add `filters` as array prefix.
    requestMethod: 'input' // The default get method is validated but you can choose different one.
  );

```

With prefix: `http://0.0.0.0/api/categories?filters[name]=ossw`

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

932d ago

PHP version history (2 changes)v1.0.0PHP ^8.0

v1.0.1PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a353084a1fe18960a14d04113328bbc82a04b44d4f23b351dce4385b58f3baff?d=identicon)[hamidreza-mozhdeh](/maintainers/hamidreza-mozhdeh)

---

Top Contributors

[![hamidreza-mozhdeh](https://avatars.githubusercontent.com/u/18644565?v=4)](https://github.com/hamidreza-mozhdeh "hamidreza-mozhdeh (18 commits)")

### Embed Badge

![Health badge](/badges/hamidreza-mozhdeh-filter-by-query-string/health.svg)

```
[![Health](https://phpackages.com/badges/hamidreza-mozhdeh-filter-by-query-string/health.svg)](https://phpackages.com/packages/hamidreza-mozhdeh-filter-by-query-string)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11223.5M33](/packages/anourvalar-eloquent-serialize)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3417.0k](/packages/duncanmcclean-statamic-cargo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
