PHPackages                             rasyidly/laravel-model-query - 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. rasyidly/laravel-model-query

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

rasyidly/laravel-model-query
============================

Advanced laravel dynamic model queries

v1.1.0(2y ago)130MITPHP

Since Dec 3Pushed 2y agoCompare

[ Source](https://github.com/rasyidly/laravel-model-query)[ Packagist](https://packagist.org/packages/rasyidly/laravel-model-query)[ RSS](/packages/rasyidly-laravel-model-query/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

Laravel Model Query
===================

[](#laravel-model-query)

Advanced laravel dynamic model queries

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

[](#installation)

Install this package with composer

```
composer require rasyidly/laravel-model-query
```

Usage
-----

[](#usage)

There are several features including this package. To use all features, use this as trait as following example.

```
use Rasyidly/ModelQuery/ModelQuery;

class User extends Model
{
    /**
     * This will load Loadable, Searchable, Sortable, and Trashable Traits in single load.
     */
    use ModelQuery;
}
```

#### OR, manually use single feature as trait

[](#or-manually-use-single-feature-as-trait)

Just use feature/s as a trait to your Model, e.g.:

```
use Rasyidly/ModelQuery/Loadable/Loadable;
use Rasyidly/ModelQuery/Searchable/Searchable;

class User extends Model
{
    use Loadable, Searchable;
}
```

Features
--------

[](#features)

There are main feature of this package features and how-to-use.

### 📃 Loadable

[](#-loadable)

Loadable is a trait that functions to load eloquent relations based on the `$loadable` property defined in the Model, like this:

```
class Post extends Model
{
    use ModelQuery;

    public $loadable = [
        'author.profile', 'commentable'
    ];

    public function author (): BelongsTo { ... }
}
```

If the relationship is not found in the definition, it will not give an error. Unless you give the wrong definition of the relationship, it is fatal.

Then, for use in the Controller, as follows:

#### Eager loading

[](#eager-loading)

```
// posts?load=author,commentable

$query = ['author', 'commentable'];

$posts = Post::loadable($request->query('load'))->get();
```

This will produce a collection/object of `Post` with the relations `author` and `commentable` by eager loading. Make sure the query is separated with comma if load more than one relation, so that loadable accepts array parameters according to Loadable's terms.

#### Lazy loading

[](#lazy-loading)

It's the same, the only difference is that it will be loaded when it becomes an object (not a query builder a.k.a. n+1)

```
// posts?load=author.profile

$post = Post::find(1);

$query = ['author.profile'];
$post = $post->loadable($request->query('load'));
```

This will load the `author.profile` relation as per the `$loadable` definition.

### 📃 Searchable

[](#-searchable)

Searchable is a trait that functions to search for specific text based on the `$searchable` property defined in the Model, like this:

```
class Post extends Model
{
    use ModelQuery;

    public $searchable = [
        'title', 'description', 'author.name'
    ];

    public function author (): BelongsTo { ... }
}
```

With this searchable definition, in sequence, it will search for a collection of Posts where `title like % ... %`, and so on. You can also use a relationship like the example above, `author.name`, meaning it will look for `name` in the `author` relationship

Then, for use in the Controller, as follows:

```
// posts?search=Hello

$posts = Post::searchable($request->query('search'))->get();
```

The result of the syntax above is to search for posts in the column defined by `$searchable`, in this case it will search like "%Hello%" in the `title` or `description` column or `name` in the `author` relationship

### 📃 Sortable

[](#-sortable)

The sortable feature is a method for sorting by columns defined in the `$sortable` property, with the addition of `,asc` or `,desc`.

```
class Post extends Model
{
    use ModelQuery;

    public $sortable = [
        'name'
    ];
}
```

An example of implementation in the Controller is as follows:

```
// posts?sort=name,asc

$posts = Post::sortable($request->query('sort'))->get();
```

Basically, sortable is exactly like Laravel's built-in `orderBy` method, only I combine the sorting method, whether ascending or descending. This trait only accepts two ascending or descending parameters, otherwise it will ignore this method, no error will be displayed.

### 📃 Trashable

[](#-trashable)

This trait only works if the Model uses `SoftDeletes`

There are no additional special properties like in the previous trait. This will only add a special method namely `trashable($default = 'without')` which only accepts 3 parameters namely `with`, `only`, and `without`.

The implementation is as follows:

```
// posts?trash=only

$posts = Post::trashable($request->query('trash'))->get();
```

The result of the query and syntax is that it will only call `onlyTrashed` from Laravel SoftDeletes.

Contributing
------------

[](#contributing)

Contributions are always welcome, just open issues, fork and/or pull your request! Up to you!

License
-------

[](#license)

This library is open-source software licensed under the [MIT](https://choosealicense.com/licenses/mit/)

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

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

Total

2

Last Release

865d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/de0bb0acb313cf8149c0dfb5e9330df18913ebae478af1ba15e911a9fb753f31?d=identicon)[rasyidly](/maintainers/rasyidly)

---

Top Contributors

[![rasyidly](https://avatars.githubusercontent.com/u/32119513?v=4)](https://github.com/rasyidly "rasyidly (8 commits)")

---

Tags

laravelphp

### Embed Badge

![Health badge](/badges/rasyidly-laravel-model-query/health.svg)

```
[![Health](https://phpackages.com/badges/rasyidly-laravel-model-query/health.svg)](https://phpackages.com/packages/rasyidly-laravel-model-query)
```

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[staudenmeir/eloquent-json-relations

Laravel Eloquent relationships with JSON keys

1.1k5.8M24](/packages/staudenmeir-eloquent-json-relations)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[gearbox-solutions/eloquent-filemaker

A package for getting FileMaker records as Eloquent models in Laravel

6454.8k2](/packages/gearbox-solutions-eloquent-filemaker)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)

PHPackages © 2026

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