PHPackages                             maarsson/eloquent-getter - 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. maarsson/eloquent-getter

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

maarsson/eloquent-getter
========================

An Eloquent getter/filter/sorter pattern for Laravel

2.0.9(1w ago)075MITPHPPHP ^8.1

Since Sep 30Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/maarsson/eloquent-getter)[ Packagist](https://packagist.org/packages/maarsson/eloquent-getter)[ RSS](/packages/maarsson-eloquent-getter/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (12)Used By (0)

Eloqent getter package
======================

[](#eloqent-getter-package)

This package adds and extendable Eloquent model getter/filter/sorter pattern to your Laravel project.

Installation
------------

[](#installation)

1. Add package to your Laravel project: `composer require maarsson/eloquent-getter`
2. Publish config file `php artisan vendor:publish --tag=eloquent-getter-config`

Usage
-----

[](#usage)

Models can be easily filtered or sorted using the package. Filter keys and sorter methods in the request without matching function in the getter class will be ignored.

1. Create getter class to your existing model: `php artisan make:getter 'YourModel'`.
2. Add the `Maarsson\EloquentGetter\Traits\GetterableTrait` trait to the model:

    ```
    namespace App\Models;

    use Maarsson\EloquentGetter\Traits\GetterableTrait;

    class YourModel
    {
        use GetterableTrait;
    }
    ```
3. Add the required filtering method(s) to the created `YourModelGetter` class. Filter method names must be camel cased and must end with `Filter`:

    ```
    protected function nameFilter(string|null $searchString): \Illuminate\Database\Eloquent\Builder
    {
        return $this->builder->where('name', 'LIKE', '%' . $searchString . '%');
    }
    ```
4. Get the filtered collection using the `filter[]` parameter in the query

    ```
    // HTTP GET //localhost/yourmodel?filter[name]=foo
    public function index(\App\Getters\YourModelGetter $getter)
    {
        return $model
            ->filter($getter)
            ->get();
    }
    ```
5. Add the required sorting method(s) to the created `YourModelGetter` class. Sorter method names must be camel cased and must end with `Sorter`:

    ```
    protected function relatedModelDateSorter(): \Illuminate\Database\Eloquent\Builder
    {
        return RelatedModel::select('date')
            ->whereColumn('this_model_column', 'related_table.column');
    }
    ```
6. Get the sorted collection using the `sort_by` parameter in the query

    ```
    // HTTP GET //localhost/yourmodel?sort_by=related_model_date
    public function index(\App\Filters\YourModelGetter $getter)
    {
        return $model
            ->order($getter)
            ->get();
    }
    ```

### Combined filtering, sorting and paginating

[](#combined-filtering-sorting-and-paginating)

Get the filtered, sorted and paginated result by the helper methods.

```
// HTTP GET //localhost/yourmodel?filter[name]=foo&page=5&per_page=20&sort_by=related_model_date&sort_order=desc
public function index(\App\Filters\YourModelGetter $getter)
{
    return $model
        ->filter($getter)
        ->order()
        ->paginate();
}
```

The following request parameters are considered:

- `filter[]` default: `null`
- `page` default: `1`
- `per_page` default: `20`
- `sort_by`default: `'id'`
- `sort_order`default: `'asc'`

### Loading attributes and relations

[](#loading-attributes-and-relations)

You can even control the model attributes to be fetched, including the relations (and its attributes). Just use simple dot-notated array passed to the `withAttributes()` method. In this example you can also see how to combine this with the pagination.

```
    // HTTP GET //localhost/yourmodel?filter[name]=foo&page=5&per_page=20&sort_by=related_model_date&sort_order=desc
    public function index(\App\Filters\YourModelGetter $getter)
    {
        return $model
            ->filter($getter)
            ->order()
            ->paginate();
            ->through(
                fn ($item) => $item->withAttributes([
                    'id',
                    'name', // a model property
                    'finalPrice', // even a model accessor
                    'users' // a relation (with all of its attributes)
                    'users.posts:id,title', // a relations relation (with limited attributes)
                ])
            );
    }
```

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](LICENSE.md).

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance73

Regular maintenance activity

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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 ~114 days

Recently: every ~188 days

Total

10

Last Release

13d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/43791389?v=4)[Maarsson](/maintainers/maarsson)[@maarsson](https://github.com/maarsson)

---

Top Contributors

[![maarsson](https://avatars.githubusercontent.com/u/43791389?v=4)](https://github.com/maarsson "maarsson (125 commits)")

### Embed Badge

![Health badge](/badges/maarsson-eloquent-getter/health.svg)

```
[![Health](https://phpackages.com/badges/maarsson-eloquent-getter/health.svg)](https://phpackages.com/packages/maarsson-eloquent-getter)
```

###  Alternatives

[leantime/leantime

Open source project management system for non-project managers. Simple like Trello, powerful like Jira. Built with neurodiversity in mind.

11.3k4.0k](/packages/leantime-leantime)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

137236.2k8](/packages/statamic-rad-pack-runway)[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11225.5M36](/packages/anourvalar-eloquent-serialize)[duncanmcclean/statamic-cargo

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

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

PHPackages © 2026

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